<?php

class TreeCatalog {
	var $mas = array();
	var $tree_path = array();
	var $raw_tree;
	var $all_blocks = array();
	var $full_tree = array();

	function TreeCatalog () {
		global $setupdata, $mysqlclass, $usersclass, $data;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->usersclass=$usersclass;
		$this->active_langs=$this->get_active_langs();
	}

	function get_active_langs(){
		$tmp = $this->get_lang_status(array("lang"=>""));
		$result = array();
		foreach ($tmp as $k=>$v) if ($v == "on") $result[$k] = $k; 
		return $result;
	}
	
	function get_block_cache_time(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='block_cache_time'", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) == 0){
			$time = time();
			$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'block_cache_time', '".$time."')", __LINE__, __FILE__);
			return $time;
		} else {
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			return $rows['value'];
		}
	}
	
	function save_all_blocks($blocks){
		if (isset($this->setupdata['cache']['block']['type']) && isset($this->setupdata['cache']['block']['path'])){
			$handle = fopen($this->setupdata['cache']['block']['path'], "w");
			$result = var_export($blocks, true); 
			fwrite($handle, "<?\n\$cached_block = ");
			fwrite($handle, $result);
			fwrite($handle, ";\n?>");
			fclose($handle);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='block_cache_time'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'block_cache_time', '".time()."')", __LINE__, __FILE__);
			else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'block_cache_time'", __LINE__, __FILE__);
		}
	}
	
	function get_all_blocks($caching = "0"){
		if (isset($this->setupdata['cache']['block']['type']) && isset($this->setupdata['cache']['block']['path']) && ($caching == "0")){
			if (file_exists($this->setupdata['cache']['block']['path'])) {
				require $this->setupdata['cache']['block']['path'];
				if ((!isset($cached_block)) || (!is_array($cached_block))) $this->get_all_blocks(1);
				else $this->all_blocks = $cached_block;
			} else $this->get_all_blocks(1);
		} else {
			$result = array();
			$sql = $this->mysqlclass->mysql_my_query("
				SELECT `t1`.`dlink` `template`, `t1`.`parent` `block_parent`, `t1`.`pos` `block_pos`, `t1`.`name` `block_realname`, `t2`.`id` `block_id`, `t2`.`name` `block_name`, `t2`.`type` `block_type`, `t3`.`name` `data_name`, `t3`.`type` `data_type`, `t3`.`value` `data_value`
				FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."catalog` `t2`, `".$this->setupdata['table_prefix']."data` `t3`
				WHERE `t1`.`type`='block' AND `t1`.`lang`='".$_SESSION['lang']."' AND `t1`.`pos` = `t2`.`id` AND `t3`.`parent`=`t2`.`id`
			", __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				if (!isset($result[$row['block_id']."_".$row['template']])){
					$result[$row['block_id']."_".$row['template']]['id'] = $row['block_id'];
					$result[$row['block_id']."_".$row['template']]['parent'] = $row['block_parent'];
					$result[$row['block_id']."_".$row['template']]['pos'] = $row['block_pos'];
					$result[$row['block_id']."_".$row['template']]['realname'] = $row['block_realname'];
					$result[$row['block_id']."_".$row['template']]['template'] = $row['template'];
					$result[$row['block_id']."_".$row['template']]['name'] = stripslashes($row['block_name']);
					$result[$row['block_id']."_".$row['template']]['type'] = $row['block_type'];
				}
				$result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['type'] = $row['data_type'];
				if (($row['data_type'] != 'form') and ($row['data_type'] != 'vote')){
					$result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'] = stripslashes($row['data_value']);
				} elseif ($row['data_type'] == 'vote'){
					$parent = $this->get_field_id($row['data_name'], $row['block_id']);
					$tmp = $this->get_form_elem_data_guest($parent);
					if ($tmp != NULL) $result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'] = $tmp;
					if (is_array($result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'])){
						$s = 0;
						foreach($result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'] as $elem){
							if (isset($elem['value'])) $elem['value'] = intval($elem['value']);
							else $elem['value'] = 0;
							$s += $elem['value'];
						}
						$last_key_summ = 0;
						foreach($result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'] as $key=>$value){
							if (isset($value['value'])) $value['value'] = intval($value['value']);
							else $value['value'] = 0;
							if ($s > 0)	$result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'][$key]['procents'] = round(($value['value']*100)/$s, 2);
							else $result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'][$key]['procents'] = 0;
							$last_key = $key;
							$last_key_summ += $result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'][$key]['procents'];
						}
						if (($last_key_summ != 100) && ($last_key_summ > 0)) {
							$result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'][$last_key]['procents'] = $result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'][$last_key]['procents'] + (100 - $last_key_summ);
						}
					}
				} else {
					$parent = $this->get_field_id($row['data_name'], $row['block_id']);
					$tmp = $this->get_form_elem_data_guest($parent);
					if ($tmp != NULL) $result[$row['block_id']."_".$row['template']]['data'][$row['data_name']]['value'] = $tmp;
				}
			}
			$this->mysqlclass->mysql_my_free_result($sql);
			$this->save_all_blocks($result);
			$this->all_blocks = $result;
		}
	}
	
	function del_block($array){
		foreach($array as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id` = ".$elem, __LINE__, __FILE__);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."data` WHERE parent='".$elem."'", __LINE__, __FILE__);
				while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
					$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$row['id']." AND `group`='".$row['type']."'", __LINE__, __FILE__);
				}
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."data` WHERE `parent` = ".$elem, __LINE__, __FILE__);				
			}
		}
	}
	
	function del_comment_from_db($arr){
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `nr` = ".$elem, __LINE__, __FILE__);		
			}
		}
	}
	
	function set_comment_cenzure($parent, $nr, $action){
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."comments_data` SET `cenzure`=".$action." WHERE `nr` = ".$nr." AND `parent`=".$parent, __LINE__, __FILE__);		
	}

	function mod_coments_to_db($nr, $parent, $arr){
		foreach($arr as $elem){
			$elem['value'] = strip_tags($elem['value']);
			$elem['value'] = addslashes($elem['value']);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `nr`=".$nr." AND `parent`=".$parent." AND `name`='".$elem['name']."'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."comments_data` (`id`,`nr`,`parent`,`name`,`value`,`type_of_value`,`date`) VALUES	('', ".$nr.", '".$parent."', '".$elem['name']."', '".$elem['value']."', '".$elem['type']."', '".date('YmdHis')."')", __LINE__, __FILE__);
			else $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."comments_data` SET `value`='".$elem['value']."' WHERE `parent` = ".$parent." AND `nr`=".$nr." AND `name`='".$elem['name']."'", __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_free_result($sql);
		}
	}
	
	function save_coments_to_db($parent, $arr){
		$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`nr`) FROM `".$this->setupdata['table_prefix']."comments_data`", __LINE__, __FILE__);		
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($row == 0) $nr  = 0;
		else {
			$row = $this->mysqlclass->mysql_my_fetch_array($sql);
			$nr = $row[0]  + 1;
		}
		foreach($arr as $elem){
			$elem['value'] = strip_tags($elem['value']);
			$elem['value'] = addslashes($elem['value']);
			if (strlen($elem['value']) > 0) {
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."comments_data` (`id`,`nr`,`parent`,`name`,`value`,`type_of_value`,`date`) VALUES	('', ".$nr.", '".$parent."', '".$elem['name']."', '".$elem['value']."', '".$elem['type']."', '".date('YmdHis')."')", __LINE__, __FILE__);
			}
		}
		$this->mysqlclass->mysql_my_free_result($sql);
	}

	function get_comments_fields($id){
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `type` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t2`.* FROM `".$this->setupdata['table_prefix']."discribe` `t1`, `".$this->setupdata['table_prefix']."discribe` `t2`
			WHERE `t1`.`type`='".$row['type']."' AND `t1`.`group`='comment' AND `t2`.`type`=`t1`.`type_of_value`
		", __LINE__, __FILE__);		
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}

	function is_can_vote($parent, $data){
		$host = $_SERVER['REMOTE_ADDR'];
		if ($data['block_vote_ip']['value'] == 'on'){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."vote_logs` WHERE `host`='".$host."' AND `parent`=".$parent, __LINE__, __FILE__);				
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) return 1;
		}
		if ($data['block_vote_cookie']['value'] == 'on') {
			if ((isset($_COOKIE["vote_cookie_".$parent])) && ($_COOKIE["vote_cookie_".$parent] == "on")) return 1;
		}
		return 0;
	}

	function save_vote_to_db($parent, $arr){
		if (!isset($parent) || (strlen($parent) < 1)) return 10;
		$host = $_SERVER['REMOTE_ADDR'];
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."data` WHERE `parent`=".$parent." AND `type`='vote'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($row > 0) {
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$row['id'], __LINE__, __FILE__);
			while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql2, MYSQL_ASSOC)) {
				$data[$rows['name']]['value'] = $rows['value'];
			}
			$array = array();
			foreach($data as $key=>$value){
				$tmp = explode("_", $key, 2);
				$array[$tmp[0]] = $value['value'];
			}
		} else return 10;
		$data = $this->get_item_content($parent);
		if ($data['block_vote_active']['value'] == 'on'){
			if ($this->is_can_vote($parent, $data) == 1) return 11;
			$question = addslashes(trim($data['block_vote_question']['value']));
			foreach($arr as $value)	{
				if (array_key_exists($value, $array)){
					$array[$value] = addslashes($array[$value]);
					$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$row['id']." AND `name`='".$value."_param01' AND `group`='vote'", __LINE__, __FILE__);
					$num = $this->mysqlclass->mysql_my_num_rows($sql);
					if ($num > 0) $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."object_data` SET `value`=`value`+1 WHERE `parent` = ".$row['id']." AND `name`='".$value."_param01' AND `group`='vote'", __LINE__, __FILE__);
					else $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', ".$row['id'].", '".$value."_param01', '1', 'text', 0, 'vote')", __LINE__, __FILE__);
					$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."vote_logs` (`id`,`parent`,`question`,`host`,`date`,`answer`) VALUES ('', '".$parent."', '".$question."', '".$host."', '".date('YmdHis')."', '".$array[$value]."')", __LINE__, __FILE__);
					if ($data['block_vote_type']['value'] != 'checkbox') break 1;
				}
			}
			if (!setcookie("vote_cookie_".$parent, "on", time()+9999999)) return 10;		
			return 0;
		} else return 12;
	}
	
	function save_form_to_db($parent, $form_id, $arr, $files){
		if (!isset($_SESSION['login_user'])) $_SESSION['login_user'] = 0;
		$info = $this->get_item_info($parent);
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."form_post_catalog` (`id`,`name`,`hide`,`pos`,`parent`, `depth`, `type`,`time_create`,`time_modif`,`lang`,`groupe`,`dlink`,`author`,`editor`, `block_id`)
				VALUES ('', '".mysql_real_escape_string($info['name'])."', '1', '0', '".$parent."', '0', 'default', '".date("YmdHis")."', '".date("YmdHis")."', '".strtolower($_SESSION['lang'])."', '', '', '".$_SESSION['login_user']."', '".$_SESSION['login_user']."', '".$form_id."')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$form_parent = $this->mysqlclass->mysql_my_insert_id();
		foreach($arr as $key=>$elem){
			$pos = 0;
			$t_key = explode("_", $key);
			$pos = intval($t_key[count($t_key)-1]);
			if (!isset($elem['value'])) $elem['value'] = " ";
			if (!isset($elem['label'])) $elem['label'] = "";
			if (!isset($elem['type'])) $elem['type'] = "";
			if ($elem['type'] != 'file'){
				if (is_array($elem['value'])){
					$elem['label'] = htmlentities(addslashes($elem['label']));
					$elem['type'] = htmlentities(addslashes($elem['type']));
					foreach($elem['value'] as $key=>$value){
						$elem['value'] = addslashes($elem['value']);
						$sql = "INSERT INTO `".$this->setupdata['table_prefix']."form_post_data` (`id`,`parent`,`name`,`value`, `type`, `pos`) VALUES ('', '".$form_parent."', '".$elem['label']."', '".$key."', '".$elem['type']."', '".$pos."')";
						$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
					}
				} else {
					if ($elem['value'] != " ") $elem['value'] = trim($elem['value']);
					$elem['label'] = htmlentities(addslashes($elem['label']));
					$elem['type'] = htmlentities(addslashes($elem['type']));
					$elem['value'] = addslashes($elem['value']);
					$sql = "INSERT INTO `".$this->setupdata['table_prefix']."form_post_data` (`id`,`parent`,`name`,`value`, `type`, `pos`) VALUES ('', '".$form_parent."', '".$elem['label']."', '".$elem['value']."', '".$elem['type']."', '".$pos."')";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				}
			} else {
				if (isset($this->setupdata['vars']['is_form_save_image']) && $this->setupdata['vars']['is_form_save_image'] == 1){
					if (isset($files[$key]) && ($files[$key]['size'] > '0') && ($files[$key]['error'] == 0)){
						$file = $files[$key];
						$x = explode(".",$file['name']); $x = $x[count($x)-1];
						$s = md5("xxx-".$key.$form_parent."-ooo");
						if (isset($this->setupdata['file_size']) && ($file['size']/1024 > $this->setupdata['file_size'])) {}
						else {
							if (copy($file['tmp_name'], $this->setupdata['binary_path']."/forms/".$s.".".$x));
							$elem['value'] = $s.".".$x;
						}

					}
					if ($elem['value'] != " ") $elem['value'] = trim($elem['value']);
					$elem['label'] = htmlentities(addslashes($elem['label']));
					$elem['type'] = htmlentities(addslashes($elem['type']));
					$elem['value'] = addslashes($elem['value']);
					$sql = "INSERT INTO `".$this->setupdata['table_prefix']."form_post_data` (`id`,`parent`,`name`,`value`, `type`, `pos`) VALUES ('', '".$form_parent."', '".$elem['label']."', '".$elem['value']."', '".$elem['type']."', '".$pos."')";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				}
			}
		}
	}
	
	function del_form_to_db($arr){
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."form_post_catalog` WHERE id = ".$elem, __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."form_post_data` WHERE parent = ".$elem, __LINE__, __FILE__);		
			}
		}
	}
	
	function change_form_status($id, $status, $usr){
		if (isset($id) and ($id > 0) and isset($status) and ($status >= 0)){
			if ($usr < 9999999)	$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."form_post_catalog` SET `depth`=".$status.", time_modif='".date("YmdHis")."', editor=".$usr." WHERE id = ".$id, __LINE__, __FILE__);
			else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."form_post_catalog` SET `depth`=".$status.", time_modif='".date("YmdHis")."' WHERE id = ".$id, __LINE__, __FILE__);
		}
	}
	
	function change_form_visible($id, $usr){
		if (isset($id) and ($id > 0)){
			if ($usr < 9999999)	$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."form_post_catalog` SET `hide`=0, time_modif='".date("YmdHis")."', editor=".$usr." WHERE id = ".$id, __LINE__, __FILE__);
		}
	}
	
	function send_form_to_mail($body_html, $body_text, $arr, $files){
		if ((isset($_SERVER['HTTP_REFERER']) && (strpos($_SERVER['HTTP_REFERER'], $this->setupdata['base_href']) !== false) && (strpos($_SERVER['HTTP_REFERER'], $this->setupdata['base_href']) == 0)) || ($_GET['admin'] == 1)){
			
			// origins 
			$__form_subject = $arr['form_subject'];
			$__html = $body_html;
			
			if (isset($this->setupdata['mail_separator'])) $m_sep = $this->setupdata['mail_separator'];
			else $m_sep = "\r\n";
			
			$m_sep = "\n";
			
			if (strlen($body_text) == 0) $body_text = " ";
			if (strlen($body_html) == 0) $body_html = " ";
			$un    = strtoupper(uniqid(time()));
			$arr['form_to'] = trim($arr['form_to']);
			$mail_array = preg_split("/[\s,;]+/", $arr['form_to'], -1, PREG_SPLIT_NO_EMPTY);
			if (count($mail_array) > 1){
				$arr['form_to'] = "";
				foreach($mail_array as $mail){
					if (preg_match('/^\".+\"\s\<[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}\>/i', $mail) || preg_match('/[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}/i', $mail)){
						$arr['form_to'] .= trim($mail).", ";
					}
				}
			}
			$arr['form_to'] = trim($arr['form_to']);
			if (substr($arr['form_to'], strlen($arr['form_to'])-1, 1) == ',') $arr['form_to'] = substr($arr['form_to'], 0, strlen($arr['form_to'])-1);
			
			if (!isset($arr['form_from'])) {
				$site_info = $this->_get_site_settings();
				if (isset($site_info['site_email01']) && (strlen($site_info['site_email01'])>0) && (preg_match('/^\".+\"\s\<[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}\>/i', $site_info['site_email01']) || preg_match('/[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}/i', $site_info['site_email01']))){
					$arr['form_from'] = $site_info['site_email01'];
				} elseif (isset($site_info['site_webmaster_email01']) && (strlen($site_info['site_webmaster_email01'])>0) && (preg_match('/^\".+\"\s\<[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}\>/i', $site_info['site_webmaster_email01']) || preg_match('/[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}/i', $site_info['site_webmaster_email01']))){
					$arr['form_from'] = $site_info['site_webmaster_email01'];
				} else {
					$arr['form_from'] = $_SERVER["SERVER_NAME"]."@".$_SERVER["SERVER_NAME"].".com";
				}
			}
			
			/*
			$head  = "From: ".$arr['form_from'].$m_sep; 
			$encoded = chunk_split(bin2hex($arr['form_subject']), 2, '='); 
			$encoded = "=".strtoupper(substr($encoded, 0, strlen($encoded) - 1));
			$arr['form_subject'] = '=?UTF-8?Q?'.$encoded.'?=';
			$head .= "Subject: ".$arr['form_subject'].$m_sep; 
			$head .= "Reply-To: ".$arr['form_from'].$m_sep; 
			$head .= "X-Mailer: JustPageIt Mailer 0.1".$m_sep; 
			$head .= "Mime-Version: 1.0".$m_sep; 
			if (count($files) > 0){
				$un1    = strtoupper(uniqid(time())); 
				$head .= "Content-Type: multipart/mixed; \n\t boundary=\"----=_NextPart_".$un."\"".$m_sep.$m_sep; 
				$body  = "------=_NextPart_".$un.$m_sep;
				$body .= "Content-Type: multipart/alternative;\n\t"; 
				$body .= "boundary=\"----=_NextPart_".$un1."\"".$m_sep.$m_sep; 
				$body .= "------=_NextPart_".$un1.$m_sep."Content-type: text/plain; charset=\"utf-8\"".$m_sep."Content-Transfer-Encoding: base64".$m_sep.$m_sep; 
				$body_text = chunk_split(base64_encode($body_text), 76, $m_sep);

				if ((substr($body_text, strlen($body_text)-2, 2) != "\r\n") && (substr($body_text, strlen($body_text)-1, 1) != "\n")){
					if (substr($body_text, strlen($body_text)-2, 2) != "\r\n") $body_text .= "\r\n";
					elseif (substr($body_text, strlen($body_text)-1, 1) != "\n") $body_text .= "\n";
				}

				$body .= $body_text; 
				$body .= "------=_NextPart_".$un1.$m_sep."Content-Type: text/html; charset=\"utf-8\"".$m_sep."Content-Transfer-Encoding: base64".$m_sep.$m_sep; 
				$body_html = chunk_split(base64_encode($body_html), 76, $m_sep);;
				
				if ((substr($body_html, strlen($body_html)-2, 2) != "\r\n") && (substr($body_html, strlen($body_html)-1, 1) != "\n")){
					if (substr($body_html, strlen($body_html)-2, 2) != "\r\n") $body_html .= "\r\n";
					elseif (substr($body_html, strlen($body_html)-1, 1) != "\n") $body_html .= "\n";
				}
				
				$body .= $body_html;
				$body .= "------=_NextPart_".$un1."--".$m_sep.$m_sep;
				foreach($files as $file){
					if (($file['size'] > '0') and ($file['error'] == 0)){
						$handle = fopen($file['tmp_name'],"rb"); 
						$body .= "------=_NextPart_".$un.$m_sep;
						$body .= "Content-Type: ".$file['type'].";"; 
						$body .= "name=\"".$file['name']."\"".$m_sep; 
						$body .= "Content-Transfer-Encoding:base64".$m_sep; 
						$body .= "Content-Disposition:attachment; filename=\"".basename($file['name'])."\"".$m_sep.$m_sep; 
						$body .= chunk_split(base64_encode(fread($handle, $file['size'])), 76, $m_sep);
					}
				}
				$body .= "------=_NextPart_".$un."--".$m_sep;
			} else {
				$head .= "Content-Type: multipart/alternative; \n\tboundary=\"----=_NextPart_".$un."\"".$m_sep.$m_sep; 
				$body = "------=_NextPart_".$un.$m_sep."Content-type: text/plain; charset=\"utf-8\"".$m_sep."Content-Transfer-Encoding: base64".$m_sep.$m_sep; 
				$body_text = chunk_split(base64_encode($body_text), 76, $m_sep);
				
				if ((substr($body_text, strlen($body_text)-2, 2) != "\r\n") && (substr($body_text, strlen($body_text)-1, 1) != "\n")){
					if (substr($body_text, strlen($body_text)-2, 2) != "\r\n") $body_text .= "\r\n";
					elseif (substr($body_text, strlen($body_text)-1, 1) != "\n") $body_text .= "\n";
				}
				
				$body .= $body_text; 
				$body .= "------=_NextPart_".$un.$m_sep."Content-Type: text/html; charset=\"utf-8\"".$m_sep."Content-Transfer-Encoding: base64".$m_sep.$m_sep; 
				$body_html = chunk_split(base64_encode($body_html), 76, $m_sep);
				
				if ((substr($body_html, strlen($body_html)-2, 2) != "\r\n") && (substr($body_html, strlen($body_html)-1, 1) != "\n")){
					if (substr($body_html, strlen($body_html)-2, 2) != "\r\n") $body_html .= "\r\n";
					elseif (substr($body_html, strlen($body_html)-1, 1) != "\n") $body_html .= "\n";
				}
				
				$body .= $body_html;
				$body .= "------=_NextPart_".$un."--".$m_sep;
			}
			*/
			
			if (count($files) > 0){
						
				$un    = strtoupper(uniqid(time()));
				
				$mime_boundary = "==Multipart_Boundary_x{$un}x"; 
				$head = 'From: '.$arr['form_from'].'';
				$head = 'Reply-To: '.$arr['form_from'].'';
				$head .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
				$__html .= "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"utf-8\"\n" ."Content-Transfer-Encoding: 7bit\n\n" . $__html . "\n\n"; 
				
				foreach($files as $file){
					if (($file['size'] > '0') and ($file['error'] == 0)){
						$__html .= "--{$mime_boundary}\n";
						$fp =    @fopen($file['tmp_name'],"rb");
						$data =    @fread($fp,$file['size']);
						@fclose($fp);
						$data = chunk_split(base64_encode($data));
						$__html .= "Content-Type: ".$file['type']."; name=\"".basename($file['name'])."\"\n" .
						"Content-Description: ".basename($file['name'])."\n" .
						"Content-Disposition: attachment;\n" . " filename=\"".basename($file['name'])."\"; size=".$file['size'].";\n" .
						"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
					}
				}
				$__html .= "--{$mime_boundary}--";
			
			}
			else
			{
				$head  = 'MIME-Version: 1.0' . "\r\n";
				$head .= 'Content-type: text/html; charset=utf-8' . "\r\n";
				$head .= 'From: '.$arr['form_from'].'' . "\r\n" . 'Reply-To: '.$arr['form_from'].'' . "\r\n";
			}
			
			if (isset($arr['form_to']) && (strlen($arr['form_to'])>4)){
			
				$mass_to = explode(",", $arr['form_to']);
				if (is_array($mass_to) && (count($mass_to) > 0)){
					
					foreach($mass_to as $email) {
						$email = trim($email);
						$res = @mail($email, $__form_subject, $__html, $head); 
						//$res = @mail($email, $arr['form_subject'], $body, $head); 
					}
				}
				return $res;
			} else return 0;
		} else {
			return 0;
		}
	}
	
	function get_field_id($name, $parent){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."data` WHERE `name` = '".$name."' AND `parent` ='".$parent."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $info['id'];
	}

	function get_form_elem_data($parent){
		$rows = array();
		$forms = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE parent='".$parent."' AND ((`name` LIKE '%_param01') OR (`name` LIKE '%_type01') OR (`name` LIKE '%_name01') OR (`name` LIKE '%_value01') OR (`name` LIKE '%_req01') OR (`name` LIKE '%_mail01') OR (`name` LIKE '%_text01')) ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$nr = substr($row['name'], 0, strpos($row['name'], "_"));
			$forms[$nr][$row['name']]['id'] = $row['id'];
			$forms[$nr][$row['name']]['name'] = stripslashes($row['name']);
			$forms[$nr][$row['name']]['type'] = $row['type'];
			$forms[$nr][$row['name']]['value'] = stripslashes($row['value']);
		}
		$this->mysqlclass->mysql_my_free_result($sql);
		ksort($forms); reset($forms);
		return $forms;
	}
	
	function get_form_elem_data_guest($parent){
		$rows = array();
		$forms = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`='".$parent."' ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$nr = substr($row['name'], 0, strpos($row['name'], "_"));
			if (is_numeric($nr)){
				$name = $row['name'];
				if (strpos($row['name'], "name")) $name = 'label';
				elseif (strpos($row['name'], "type")) $name = 'type';
				elseif (strpos($row['name'], "req")) $name = 'req';
				elseif (strpos($row['name'], "param")) $name = 'values';
				elseif (strpos($row['name'], "text")) $name = 'text';
				if ($name != 'values') $forms[$nr][$name] = stripslashes($row['value']);
				else {
					$row['value'] = @ereg_replace(";+$", "", $row['value']);
					$forms[$nr][$name] = explode(";", $row['value']);
					$forms[$nr]['value'] = stripslashes($row['value']);
				}
			}
		}
		$this->mysqlclass->mysql_my_free_result($sql);
		ksort($forms); reset($forms);
		return $forms;
	}
	
	function del_form_elem_data($form_id, $group){
		$sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."object_data` WHERE parent = ".$form_id." AND `group`='".$group."'", __LINE__, __FILE__);		
	}
	
	function mod_form_elem_data($page_form_id, $parent, $name, $value, $group){
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET time_modif='".date("YmdHis")."' WHERE `id`=".$page_form_id, __LINE__, __FILE__);
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent` = ".$parent." AND `name`='".$name."' AND `type`='text' AND `group`='".$group."'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		$name = addslashes($name);
		$value = addslashes($value);
		if ($row == 0) {
			$type = "text"; $pos = 0;
			$sql = "INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`, `type`, `pos`, `group`) VALUES ('', '".$parent."', '".$name."', '".$value."', '".$type."', '".$pos."', '".$group."')";
			$res = $this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		} else {
			$sql = "UPDATE `".$this->setupdata['table_prefix']."object_data` SET value='".$value."' WHERE parent = ".$parent." and name='".$name."' AND `group`='".$group."'";
			$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		}
		$this->mysqlclass->mysql_my_free_result($sql);
	}
	
	function get_block_tempalate($arr){
		for ($i=0; $i<count($arr); $i++) {
			$rows = array();
			$temp_dir = $this->setupdata['smarty_guest_block_template_dir']."/".$arr[$i]['type'];
			if (is_dir($temp_dir)) {
				if ($dir = opendir($temp_dir)){
					while (($file = readdir($dir)) !== false) if (is_file($temp_dir."/".$file)) array_push($rows, $file);
					closedir($dir);
				}
			}
			$arr[$i]['templates'] = $rows;
		}
		return $arr;
	}

	
	function get_block_parent($parent, $name, $first=0){
		if (!isset($parent) or (strlen($parent)<=0)) return false;
		if ($first == 0) $name = urlencode($name);
		$name = addslashes($name);
		
		    $blocks = array();
      $sql = $this->mysqlclass->mysql_my_query("
				SELECT `name`,`dlink`, `parent`, `pos`
				FROM `".$this->setupdata['table_prefix']."catalog`
				WHERE `type`='block' AND `lang`='".$_SESSION['lang']."' AND `name`='".$name."'
			", __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$blocks[$row["parent"]] = $row;
			}
		
      if (isset($blocks[$parent]["pos"])&& $blocks[$parent]["pos"]!=-1) {
        $search_in_tree = false;
      }
      else {
        $search_in_tree = true;

      // Jeigu neranda medzio, traukia medi + elementus
      if (!$this->full_tree)
      {
        $temp = array();
        $temp = $this->gettreetypes('tree');
        $temp = array_merge($temp,$this->getelemenstypes());
		    $this->maketree(0, strtolower($_SESSION['lang']), $temp);
        $this->full_tree = $this->mas;
		    unset($temp,$this->mas);
      }
      
      
      // Paima medzio saka pagal einamaji puslapi
      foreach ($this->full_tree as $item)
      {
        if ($item["id"]==$parent)
        {
          $tree = $item;
          break;
        }
      }
      
      // Iesko bloko busenos pagal parent' rekursijos budu (rekursija vykdoma per is auksciau paimto medzio sakos parent'us)
      $parent = 0;
      if ($tree)
      {
        $item = $tree;
        while ($item["id"] !== false)
        {
          if (isset($blocks[$item["id"]]["pos"]) && $blocks[$item["id"]]["pos"]==0) { return false; } // jeigu randa kad pas parent'a isjungtas blokas, iseina funkcijos, nieko negrazina
          elseif (!isset($blocks[$item["id"]]["pos"])) true; // jeigu neranda bloko iesko toliau (_default apveldi)
          elseif ($blocks[$item["id"]]["pos"]==-1) true; // jeigu paveldi iesko toliau
          elseif (isset($blocks[$item["id"]]["pos"])) { $parent = $item["id"]; break; } // Jeigu randa parent'a su ijungtu bloku $parent = to parent puslapio id
          
          if ($item["id"]==0) break; // jeigu sakos parent'ai baigesi iseina i ciklo ir $parent=0
          else $item = $item["parent"]; // kartoja cikla
        }
      }
      
      }
      $block = "";
      $block = $blocks[$parent]; // Ima bloka pagal gauta parent'a auksciau
      $result = array(
        "id" => $block["pos"],
        "template" => $block["dlink"],
        "search_in_tree" => $search_in_tree
      );
			return $result;

	}
	
	
	/*function get_block_parent_in_tree($parent, $array, $mas){
		if (count($mas) == 1) return current($mas);
		if (count($array) == 0){
			global $data;
			$array = array_reverse($data['path']);
			reset($array);
		}
		
		$i = count($array);
		while ($i >= 0){
			$value = current($array);
			if (isset($mas[$value['id']])){
				$result = $mas[$value['id']];
				$array == array();
			} else {
				$tmp = array_slice($array, 1);
				$array = $tmp;
				reset($array);
			}
			$i--;
		}
		
		if (!isset($result) && isset($mas[0])) return $mas[0];
		return $result;
	}
	

	function get_block_parent($parent, $name, $first=0){
		if (!isset($parent) or (strlen($parent)<=0)) return false;
		if ($first == 0) $name = urlencode($name);
		$name = addslashes($name);
		
		if ($parent == 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `name`='".$name."' and `parent`=0 and `type`='block' and `lang`='".$_SESSION['lang']."'", __LINE__, __FILE__);			
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$result['id'] = $rows['pos'];
				$result['template'] = $rows['dlink'];
				return $result;
			} else return false;
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `name`='".$name."' and `type`='block' and `lang`='".$_SESSION['lang']."'", __LINE__, __FILE__);
			while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
				if ($rows['pos'] > 0) {
					if ($rows['parent'] == $parent){
						$result['id'] = $rows['pos'];
						$result['template'] = $rows['dlink'];
						return $result;
					} else {
						$result[$rows['parent']]['id'] = $rows['pos'];
						$result[$rows['parent']]['template'] = $rows['dlink'];
					}
				}
			}
			return $this->get_block_parent_in_tree($parent, array(), $result);
		}
		
	}*/


	
	function set_block_parent($parent, $name, $id, $lang, $tempalate){
		$name = urlencode($name);
		$name = addslashes($name);
		$mas[] = array('type_of_value'=>'block');
		$this->maketree($parent, $lang, $mas);
    //print_r($this->mas);
		if (count($this->mas) > 0){
			foreach($this->mas as $item) {
				if ($item['name'] == $name){
					$posid = $item['id'];
					if ($id >= 0) $sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$id.", dlink='".$tempalate."', time_modif='".date("YmdHis")."', editor='".$_SESSION['login_user']."' WHERE id = ".$posid." and name='".$name."'";
					else $sql = "DELETE FROM`".$this->setupdata['table_prefix']."catalog` WHERE id = ".$posid." and name='".$name."'";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
					$p = "";
				}
			}
			if (!isset($p)) $p = $this->treenew($parent, $name, 'block', 1, 0, $id, $lang, '', $tempalate, $_SESSION['login_user']);
		} else {
			$p = $this->treenew($parent, $name, 'block', 1, 0, $id, $lang, '', $tempalate, $_SESSION['login_user']);
		}
		return $p;
	}
	
	function get_parent_by_element_id($id, $type, $el_types){
		global $tree;
		if (in_array($type, $el_types)){
			$sql = $this->mysqlclass->mysql_my_query("
				SELECT `t2`.`id`, `t2`.`type` 
				FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."catalog` `t2`
				WHERE `t1`.`id` = ".$id." AND `t1`.`parent`=`t2`.`id`"
			, __LINE__, __FILE__);
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			return $this->get_parent_by_element_id($rows['id'], $rows['type'], $el_types);
		} else {
			foreach ($tree as $el) {
				if ($el['id'] == $id) return $el;
			}
		}
		return NULL;
	}
			
	function tree_search($str, $search_type=NULL) {
		global $data;
		$result = array();
		$search_arr = array();
		$str = stripslashes($str);
		$str = urldecode($str);
		if ($search_type==NULL){
			$search_arr = preg_split("/[\s,;\.]+/", $str, -1, PREG_SPLIT_NO_EMPTY);
			//$search_arr = preg_split("/[^\w]+/", $str, -1, PREG_SPLIT_NO_EMPTY);
			if (count($search_arr) < 1) return $result;
		} elseif ($search_type==1){
			$search_arr[] = $str;
		}
		
		$types_of_element = array();
		$types3 = $this->gettreetypes('tree');

		$types2 = "('";
		foreach ($types3 as $value) {
			if (isset($this->setupdata['vars']['no_search_type']) && (count($this->setupdata['vars']['no_search_type']) > 0)){
				if (!in_array($value['type_of_value'], $this->setupdata['vars']['no_search_type'])) $types2 .= $value['type_of_value']."', '";
			} else $types2 .= $value['type_of_value']."', '";
		}
		
		$sql9 = $this->mysqlclass->mysql_my_query("SELECT DISTINCT type_of_value FROM `".$this->setupdata['table_prefix']."discribe` WHERE `group`='element'", __LINE__, __FILE__);
		while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql9, MYSQL_ASSOC)) array_push($types_of_element, $rows['type_of_value']);

		$this->maketree(0, strtolower($_SESSION['lang']), $types3, 1);		
		$tree=$this->mas;
	
		foreach ($types3 as $value) $types5[] = $value['type_of_value'];
		$types6 = $types_of_element;
		foreach ($types_of_element as $value) {
			$types2 .= $value."', '";
		}
		$types2 .= "')";

		$search_arr[0] = addslashes($search_arr[0]);
		$search_str = "`name` LIKE '%".$search_arr[0]."%'";
		for ($i=1; $i < count($search_arr); $i++) {
			$search_arr[$i] = addslashes($search_arr[$i]);
			$search_str .= " or `name` LIKE '%".$search_arr[$i]."%'";
		}
		$sql5 = $this->mysqlclass->mysql_my_query("
			SELECT DISTINCT * FROM `".$this->setupdata['table_prefix']."catalog` 
			WHERE ".$search_str." && `type` IN ".$types2." && `lang`='".strtolower($_SESSION['lang'])."' && `hide`='1'
			ORDER BY `pos`
		", __LINE__, __FILE__);
		while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql5, MYSQL_ASSOC)) {
			if ((in_array($rows['type'], $types5)) || (in_array($rows['type'], $types6))) {
				$is_in_tree = $rows;
				if (in_array($rows['type'], $types6)) foreach ($tree as $el) if ($el['id'] == $rows['parent']) {$is_in_tree = $el;}
				foreach ($tree as $el) if ($el['id'] == $is_in_tree['id'])
				{
					if (strlen($rows['dlink'])>0) {
						if (count($this->active_langs) > 1) $cur_lang = $rows['lang']."/";
						else $cur_lang = "";
						if (strpos($rows['dlink'], "http://") === false) $rows['url'] = $this->setupdata['base_href']."/".$cur_lang.$rows['dlink'];
						else $rows['url'] = $rows['dlink'];
					} else $rows['url'] = $this->setupdata['base_href']."/"."index.php?id=".$rows['id'];
					if ($rows['type'] == 'url01') {
						$info = $this->get_field_value($rows['id'], 'static_url');
						if ((isset($info)) and (strlen($info)>0)) $rows['url'] = $info;
						$info = $this->get_field_value($rows['id'], 'url_target');
						if ((isset($info)) and (strlen($info)>0)) $rows['target'] = $info;
					}
					$rows['value'] = stripslashes($rows['name']);
					if (in_array($rows['type'], $types6)) $rows['parent'] = $el;
					else $rows['parent'] = $el['parent'];
					unset($rows['dlink']);
					unset($rows['lang']);
					unset($rows['value']);
					$result[$rows['id']] = $rows;
				}
			}
		}
		$types1 = "('text', 'textarea', 'docum', 'flashtext')";
		$search_str = "`t2`.`value` LIKE '%".$search_arr[0]."%'";
		for ($i=1; $i < count($search_arr); $i++) $search_str .= " or `t2`.`value` LIKE '%".$search_arr[$i]."%'";
		$sql1 = $this->mysqlclass->mysql_my_query("
			SELECT DISTINCT `t1`.`id`, `t1`.`name`, `t1`.`depth`, `t1`.`parent` as `page_parent`, `t2`.`parent`, `t1`.`lang`, `t1`.`dlink`, `t2`.`value`, `t1`.`type`
			FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."data` `t2`
			WHERE `t2`.`type` IN ".$types1." && `t1`.`id` = `t2`.`parent` && `t1`.`type` IN ".$types2." && `t1`.`lang`='".strtolower($_SESSION['lang'])."' && (".$search_str.") &&  `t1`.`hide`='1'
			ORDER BY `t1`.`pos`
		", __LINE__, __FILE__);
		while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql1, MYSQL_ASSOC)) {
			if ((in_array($rows['type'], $types5)) || (in_array($rows['type'], $types6))) {
				$is_in_tree = $rows;
				if (in_array($rows['type'], $types6)) {
					$is_in_tree = $this->get_parent_by_element_id($rows['id'], $rows['type'], $types6);
					/*
					if (isset($_GET['xxx'])){
						$is_in_tree = $this->get_parent_by_element_id($rows['id'], $rows['type'], $types6);
					} else {
						foreach ($tree as $el) {
							if ($el['id'] == $rows['page_parent']) $is_in_tree = $el;
						}
					}
					*/
				}
				foreach ($tree as $el) if ($el['id'] == $is_in_tree['id'])
				{
					if (strlen($rows['dlink'])>0) {
						if (count($this->active_langs) > 1) $cur_lang = $rows['lang']."/";
						else $cur_lang = "";
						if (strpos($rows['dlink'], "http://") === false) $rows['url'] = $this->setupdata['base_href']."/".$cur_lang.$rows['dlink'];
						else $rows['url'] = $rows['dlink'];
					} else {
						$rows['parent'] = $rows['page_parent'];
						$rows['url'] = $this->setupdata['base_href']."/"."index.php?id=".$rows['id'];
					}
					if ($rows['type'] == 'url01') {
						$info = $this->get_field_value($rows['id'], 'static_url');
						if ((isset($info)) and (strlen($info)>0)) $rows['url'] = $info;
						$info = $this->get_field_value($rows['id'], 'url_target');
						if ((isset($info)) and (strlen($info)>0)) $rows['target'] = $info;
					}
		//			
					$rows['name']  = stripslashes($rows['name']);
					if (in_array($rows['type'], $types6)) $rows['parent'] = $el;
					else $rows['parent'] = $el['parent'];
					
					$tmp_str = strtolower($rows['value']);
					$tmp_str = strip_tags($tmp_str);
					
					if ($tmp_pos = strpos(strtolower($tmp_str), strtolower($str)) !== false){
						$length = $this->setupdata['search_intro_lenght'];
						$length -= strlen("...");
						$pos_from = $tmp_pos-$length;
						$pos_to = $tmp_pos-strlen($str);
						if ($pos_from < 0) $pos_from = 0;
						$rows['value'] = strip_tags($rows['value']);
						
//						$rows['intro'] = $this->bite_str($rows['value'], $pos_from, $pos_to);

						$rows['intro']  = "... ";
						$rows['intro'] .= preg_replace('/^\b\w*\S*/', '', substr(stripslashes($rows['value']), $pos_from, $pos_to-$pos_from));
						$rows['intro'] .= preg_replace('/\s+?(\S+)?$/', '', substr(stripslashes($rows['value']), $pos_to, $length+1));
						$rows['intro'] .= " ...";
					}
					
					unset($rows['page_parent']);
					unset($rows['author']);
					unset($rows['editor']);
					unset($rows['time_create']);
					unset($rows['time_modif']);
					unset($rows['dlink']);
					unset($rows['lang']);
					unset($rows['value']);
					$result[$rows['id']] = $rows;
				}
			}
		}
		
		if ((isset($this->setupdata['modules']['clients']['is'])) && (is_array($result)) && (count($result)>0)){
			global $clientsclass;
			if (isset($_SESSION['client_login_id']) && (strlen($_SESSION['client_login_id']) > 0)) $client_id = $_SESSION['client_login_id'];
			else $client_id = 0;
			foreach($result as $key=>$value){
				$ids = $clientsclass->get_secure_folder_id($value);
				if ($ids > 0){
					$access = $clientsclass->is_client_access_to_page($ids, $client_id);
					if ($access != 1) unset($result[$key]);
				}
			}
		}
		
		$this->mysqlclass->mysql_my_free_result($sql);
		$this->mysqlclass->mysql_my_free_result($sql1);
		$this->mysqlclass->mysql_my_free_result($sql5);
		$this->mysqlclass->mysql_my_free_result($sql9);
		
		return $result;
	}

	function last_update_tree(){
		$type = $this->gettreetypes('tree');
		$types = "('";
		foreach ($type as $value) $types .= $value['type_of_value']."', '";
		$types .= "')";
		$sql = $this->mysqlclass->mysql_my_query("select UNIX_TIMESTAMP(max(time_modif)) FROM `".$this->setupdata['table_prefix']."catalog` WHERE type in ".$types, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows[0];
	}
	
	function get_last_update_db(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='last_db_update'", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) == 0){
			$time = time();
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'last_db_update', '".$time."')", __LINE__, __FILE__);
			return $time;
		} else {
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$this->mysqlclass->mysql_my_free_result($sql);
			return $rows['value'];
		}
	}

	function get_tree_cache_time($type){
		$time = time();
		if ($type == 'guest'){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_cache_time'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) == 0){
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_cache_time', '".$time."')", __LINE__, __FILE__);
				return $time;
			} else {
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$this->mysqlclass->mysql_my_free_result($sql);
				return $rows['value'];
			}
		} elseif ($type == 'admin'){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_admin_cache_time'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) == 0){
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_admin_cache_time', '".$time."')", __LINE__, __FILE__);
				return $time;
			} else {
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$this->mysqlclass->mysql_my_free_result($sql);
				return $rows['value'];
			}
		}
	}

	function get_item_info($id){
		if (intval($id) < 0) return NULL;
		$id = intval($id);
		$row = array();
		$query = "SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id;
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);		
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$row['name'] = stripslashes($row['name']);
			if (strlen($row['dlink'])>0){
				if (count($this->active_langs) > 1) $cur_lang = $row['lang']."/";
				else $cur_lang = "";
				if (strpos($row['dlink'], "http://") === false) $row['url'] = $this->setupdata['base_href']."/".$cur_lang.$row['dlink'];
				else $row['url'] = $row['dlink'];
			} else $row['url'] = $this->setupdata['base_href']."/"."index.php?id=".$row['id'];
			if ($row['type'] == 'url01') {
				$row1 = $this->get_field_value($row['id'], 'static_url');
				if ((isset($row1)) and (strlen($row1)>0)) $row['url'] = $row1;
				$row1 = $this->get_field_value($row['id'], 'url_target');
				if ((isset($row1)) and (strlen($row1)>0)) $row['target'] = $row1;
			}
			$this->mysqlclass->mysql_my_free_result($sql);
			return $row;
		} else return NULL;
	}
	
	function is_can_copy_page($id, $to){
		$path = $this->sqlpath($to);
		foreach ($path as $value) if ($value['id'] == $id ) return 0;
		return 1;
	}
	
	function tree_copy2($id, $to, $usr, $lang) {
		if (!isset($id) or (strlen($id) < 1)) return NULL;
		if (!isset($to) or (strlen($to) < 1)) return NULL;
		if (!isset($lang) or (strlen($lang) < 1)) return NULL;
		$submenu = $this->mysqlclass->mysql_my_query("SELECT id FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$id, __LINE__, __FILE__);
		while ($submenurow = $this->mysqlclass->mysql_my_fetch_array($submenu) ){
			$sql = $this->mysqlclass->mysql_my_query("SELECT depth FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$to, __LINE__, __FILE__);
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$depth = $rows['depth'] + 1;
			$id = $submenurow['id'];
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$rows['name'] = stripslashes($rows['name']);
			$new_i = $this->treenew($to, $rows['name'], $rows['type'], $rows['hide'], $depth, $rows['pos'], $lang, $rows['groupe'], $rows['dlink'], $rows['author'], $usr);
			$this->set_child_permision($id, $new_i);
			$dat = $this->get_item_data($id);
			if (count($dat) > 0) {
				for ($i=0; $i < count($dat); $i++){
					$this->mod_item_data($new_i, $dat[$i]['name'], addslashes($dat[$i]['value']));
				}
			}
			if ((isset($new_i)) and ($id != $new_i)) $this->tree_copy2($id, $new_i, $usr, $lang);
		} 
		$this->mysqlclass->mysql_my_free_result($submenu);
	}

	function tree_copy($id, $to, $usr, $lang) {
		if (!isset($id) or (strlen($id) < 1)) return NULL;
		if (!isset($to) or (strlen($to) < 1)) return NULL;
		if (!isset($lang) or (strlen($lang) < 1)) return NULL;
		if ($this->is_can_copy_page($id, $to) == 0) return NULL;
		$sql=$this->mysqlclass->mysql_my_query("SELECT max(pos) FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$to, __LINE__, __FILE__);
		$num = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($num > 0){
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$pos = $rows['max(pos)'] + 1;
		} else $pos = 1;
		$sql=$this->mysqlclass->mysql_my_query("SELECT `depth`, `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$to, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		if ($to == 0){
			$depth = 0;
		} else {
			$depth = $rows['depth'] + 1;
			$lang = $rows['lang'];
		}
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
		$menu = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent = ".$id, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
//		
		$rows['name'] = stripslashes($rows['name']);
		$new_id = $this->treenew($to, $rows['name'], $rows['type'], $rows['hide'], $depth, $pos, $lang, $rows['groupe'], $rows['dlink'], $rows['author'], $usr);
		$this->set_child_permision($id, $new_id);
		$dat = $this->get_item_data($id);
		if (count($dat) > 0) {
			for ($i=0; $i < count($dat); $i++){
				if ($dat[$i]['type'] == 'form') $this->mod_item_data($new_id, $dat[$i]['name'], addslashes($dat[$i]['value']), $dat[$i]['id']);
				elseif ($dat[$i]['type'] == 'vote') $this->mod_item_data($new_id, $dat[$i]['name'], addslashes($dat[$i]['value']), $dat[$i]['id']);
				else $this->mod_item_data($new_id, $dat[$i]['name'], addslashes($dat[$i]['value']));
			}
		}
		while ( $menurow = $this->mysqlclass->mysql_my_fetch_array($menu, MYSQL_ASSOC) ){
			$id = $menurow['id'];
			$new_depth = $depth + 1;
			$menurow['name'] = stripslashes($menurow['name']);
			$new_i = $this->treenew($new_id, $menurow['name'], $menurow['type'], $menurow['hide'], $new_depth, $menurow['pos'], $lang, $menurow['groupe'], $menurow['dlink'], $menurow['author'], $usr);
			$this->set_child_permision($id, $new_i);
			$dat = $this->get_item_data($id);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if (count($dat) > 0) {
				for ($i=0; $i < count($dat); $i++){
					$this->mod_item_data($new_i, $dat[$i]['name'], addslashes($dat[$i]['value']));
				}
			}
			if ((isset($new_i)) and ($id != $new_i)) $this->tree_copy2($id, $new_i, $usr, $lang);
		}
		
		$this->mysqlclass->mysql_my_free_result($sql);
		$this->mysqlclass->mysql_my_free_result($menu);
		$this->mysqlclass->mysql_my_free_result($rows);
	}

	function set_new_depth($parent, $depth, $usr){
		if (!isset($parent) or (strlen($parent) < 1)) return NULL;
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent`=".$parent, __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql)){	
			$depth_new = $depth + 1;
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET `depth`=".$depth_new.", `time_modif`='".date('YmdHis')."', `editor`='".$usr."' WHERE `id`=".$row['id'], __LINE__, __FILE__);
			$this->set_new_depth($row['id'], $depth_new, $usr);
		}
		$this->mysqlclass->mysql_my_free_result($sql);
	}
	
	function tree_move($id, $to, $usr, $arr_path) {
		if (!isset($id) or (strlen($id) < 1)) return NULL;
		if (!isset($to) or (strlen($to) < 1)) return NULL;
		if ($this->is_can_copy_page($id, $to) == 0) return NULL;
		foreach ($arr_path as $val) if ($val['id'] == $id) return NULL;
		$id = intval($id); $to = intval($to);
		$sql=$this->mysqlclass->mysql_my_query("SELECT `parent`, `pos` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);
		$old_rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$sql=$this->mysqlclass->mysql_my_query("SELECT `depth` FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$to, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$depth = $rows['depth'] + 1;
		if ($to == 0) $depth = 0;
		$sql=$this->mysqlclass->mysql_my_query("SELECT max(pos) FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$to, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		if ($rows['max(pos)'] == NULL) $pos = 1; else $pos = $rows['max(pos)'] + 1;
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$pos.", depth=".$depth.", parent=".$to.", time_modif='".date('YmdHis')."', editor='".$usr."' WHERE id = ".$id, __LINE__, __FILE__);
		$this->set_new_depth($id, $depth, $usr);

		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET `pos`=`pos`-1, `time_modif`='".date('YmdHis')."', `editor`='".$usr."' WHERE `parent` = ".$old_rows['parent']." AND `pos`>".$old_rows['pos'], __LINE__, __FILE__);
		
		$this->mysqlclass->mysql_my_free_result($sql);
	}
	 
	function post_text_parsing($str){
		$str = addslashes($str);
//		$str = eregi_replace(chr(34), "&quot", $str);
//		$str = $this->image_parsing_resize($str);
//		$str = $this->table_parsing_rezise($str);
		return $str;
	}
	
	function table_parsing_rezise($str){
		$str = eregi_replace("<table", "<table", $str);
		$str = eregi_replace("style", "style", $str);
		$str = eregi_replace("height", "height", $str);
		$str = eregi_replace("width", "width", $str);
		$tmp = $str;
		$t = explode("<table", $tmp);
		if (strpos($tmp, "<table") === FALSE) return $tmp;
		$str = "";
		for ($i=0; $i < count($t); $i++){
				if (strpos($t[$i], "style") > 0){
					$tmp1 = $t[$i];
					$tmp2 = substr($tmp1, 0, strpos($tmp1, ">"));
				  	$tmp3 = substr($tmp1, strpos($tmp1, ">"));
					$tmp2 = preg_replace("'style=\"(.*)px\" 'si", "", $tmp2);
					$t[$i] = $tmp2.$tmp3;
				}
				if ((strpos($t[$i], "height") > 0) or (strpos($t[$i], "width") > 0)){
					$tmp1 = $t[$i];
					$tmp2 = substr($tmp1, 0, strpos($tmp1, ">"));
				  	$tmp3 = substr($tmp1, strpos($tmp1, ">"));
					$tmp2 = preg_replace("'width=((.*)(\")) 'si", "", $tmp2);
					$tmp2 = preg_replace("'width=(\d+) 'si", "", $tmp2);
					$t[$i] = $tmp2.$tmp3;
				}
				if (strpos($t[$i], "border") > 0) $str .= "<table".$t[$i];
				else $str .= $t[$i];
		}
		return $str;		
	}
	
	function image_parsing_resize($str){
		$str = eregi_replace("<img", "<img", $str);
		$str = eregi_replace("src", "src", $str);
		$str = eregi_replace("width", "width", $str);
		$str = eregi_replace("height", "height", $str);
		$str = eregi_replace("px", "px", $str);
		$tmp = $str;
		$t = explode("<img", $tmp);
		if (strpos($tmp, "<img") === FALSE) return $tmp;
		$str = "";
		for ($i=0; $i < count($t); $i++){
 			if (strpos($t[$i], "src") <> FALSE){
				if (strpos($t[$i], "style") > 0){
					$tmp1 = $t[$i];
					$tmp2 = substr($tmp1, 0, strpos($tmp1, ">"));
				  	$tmp3 = substr($tmp1, strpos($tmp1, ">"));    
					$tmp21 = substr($tmp2, strpos($tmp2, "width: ")+7);
					$w = substr($tmp21, 0, strpos($tmp21, "px"));
					$tmp21 = substr($tmp2, strpos($tmp2, "height: ")+8);			
					$h = substr($tmp21, 0, strpos($tmp21, "px"));
					list(, , $t1, $t2, $t3) = explode("\"", $tmp2);
					list($t11, $t21) = explode("?", $t2);
					list($t111, $t211, $t311) = explode(":", $t21);
					list($i1, $i2) = explode(".", $t111);
					list($h1, $h2) = explode(".", $t211);
					list($w1, $w2) = explode(".", $t311);
					$str_tmp = "$t1\"$t11?i.$i2:w.$w:h.$h\"$t3";
					$p = explode(" ", $str_tmp);
					$str_tmp = "";
					for ($n=0; $n < count($p); $n++){
						if ((strpos($p[$n], 'height')===false)and(strpos($p[$n], 'width')===false)) {
							$str_tmp .= $p[$n]." ";
						}
					}
					$str .= "<img".$str_tmp.$tmp3;
				}
				elseif ((strpos($t[$i], "height") > 0) or (strpos($t[$i], "width") > 0)){
					$tmp1 = $t[$i];
					$tmp2 = substr($tmp1, 0, strpos($tmp1, ">"));
				  	$tmp3 = substr($tmp1, strpos($tmp1, ">"));    
					$tmp21 = substr($tmp2, strpos($tmp2, "height=")+7);
					$pos = strpos($tmp21, " ");
					if ($pos == false) $pos = strpos($tmp21, ">");
					$h = substr($tmp21, 0, $pos);
					$tmp21 = substr($tmp2, strpos($tmp2, "width=")+6);
					$pos = strpos($tmp21, " ");
					if ($pos == false) $pos = strpos($tmp21, ">");
					if ($pos == false) $w = substr($tmp21, 0, strlen($tmp21));
					else $w = substr($tmp21, 0, $pos);
					list($t1, $t2, $t3) = explode("\"", $tmp2);
					list($t11, $t21) = explode("?", $t2);
					list($t111, $t211, $t311) = explode(":", $t21);
					list($i1, $i2) = explode(".", $t111);
					list($h1, $h2) = explode(".", $t211);
					list($w1, $w2) = explode(".", $t311);
					$str_tmp = "$t1\"$t11?i.$i2:w.$w:h.$h\"$t3";
					$p = explode(" ", $str_tmp);
					$str_tmp = "";
					for ($n=0; $n < count($p); $n++){
						if ((strpos($p[$n], 'height')===false)and(strpos($p[$n], 'width')===false)) {
							$str_tmp .= $p[$n]." ";
						}
					}
					$str .= "<img".$str_tmp.$tmp3;
				} else {
					if (strpos($t[$i], "src") > 0) $str .= "<img".$t[$i];
					else $str .= $t[$i];
				  }
			} else $str .= $t[$i];
		}
		return $str;		
	}

	function get_item_data($id){
		$id = intval($id);
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."data` WHERE parent=".$id." ORDER BY pos", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$row['value'] = stripslashes($row['value']);
			array_push($rows, $row);
		}
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}

	function get_item_content($id){
		$dat = array();
		if (!isset($id) or (is_array($id)) or (strlen($id) <= 0)) return $dat;
		$id = intval($id);
		$mas = $this->get_item_data($id);
		foreach ($mas as $val) {
			if (isset($this->setupdata['vars']['main_type_of_value']) && in_array($val['type'], $this->setupdata['vars']['main_type_of_value'])) {
				$row = "";
			} else {
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE type_of_value='".$val['type']."'", __LINE__, __FILE__);
				$row = $this->mysqlclass->mysql_my_num_rows($sql);
				if ($row > 0) {
					$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
					$row = $row['group'];
				} else $row = "";
			}
			if ($row == ""){
				$dat[$val['name']] = array();
				$dat[$val['name']]['type'] = $val['type'];
				if (($val['type'] != 'form') and ($val['type'] != 'vote')) $dat[$val['name']]['value'] = $val['value'];
				else {
					$parent = $this->get_field_id($val['name'], $id);
					$tmp = $this->get_form_elem_data_guest($parent);
					if ($tmp != NULL) $dat[$val['name']]['value'] = $tmp;
				}
			}
		}
		return $dat;
	}

	function save_item_data($type, $parent, $name, $value){
//
		$name = addslashes($name);
		$types = $this->gettreetypes($type);
		for ($i=0; $i<count($types); $i++) {
			$row = $types[$i];
			if ($row['name'] == $name) {
				$typ = $row['type_of_value'];
				$pos = $row['pos'];
				break;
			}
		}
		if ((isset($typ)) and (isset($pos))){
			
			$sql = "INSERT INTO `".$this->setupdata['table_prefix']."data` (`id`,`parent`,`name`,`value`, `type`, `pos`) VALUES ('', '".$parent."', '".$name."', '".$value."', '".$typ."', '".$pos."')";
			$res = $this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		}
		return $this->mysqlclass->mysql_my_insert_id();
	}

	function mod_item_data($parent, $name, $value, $form=0){
		if (!isset($parent) or (strlen($parent) < 1)) return NULL;
		$type = ''; $pos = '';
		$info = $this->get_item_info($parent);
		$dat = $this->gettreetypes($info['type']);
		foreach ($dat as $val) {
			if ($val['name'] == $name) {
				$type = $val['type_of_value'];
				$pos = $val['pos'];
				break;
			}
		}
		$sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET time_modif='".date("YmdHis")."' WHERE id = ".$parent;
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."data` WHERE `parent` = ".$parent." AND `name`='".$name."' AND `type`='".$type."'", __LINE__, __FILE__);

		if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) {
			$res = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."data` (`id`,`parent`,`name`,`value`, `type`, `pos`) VALUES ('', '".$parent."', '".$name."', '".$value."', '".$type."', '".$pos."')", __LINE__, __FILE__);
			if ((($type == 'form') or ($type == 'vote')) && ($form > 0)){
				$new_id = mysql_insert_id();
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent` = ".$form, __LINE__, __FILE__);						
				while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
					$row['value'] = addslashes($row['value']);
					$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES (NULL ,  '".$new_id."',  '".$row['name']."',  '".$row['value']."',  '".$row['type']."',  '".$row['pos']."',  '".$row['group']."')", __LINE__, __FILE__);
				}
			}
		} else {
			$sql1 = "UPDATE `".$this->setupdata['table_prefix']."data` SET `value`='".$value."' WHERE `parent` = ".$parent." AND `name`='".$name."'";
			$this->mysqlclass->mysql_my_query($sql1, __LINE__, __FILE__);
		}
	}

	function treepos($do, $posid) {
		$dat = date("YmdHis");
		if ($do == 'down'){
			$query="
				SELECT 
				`t1` . * , `t2`.`type` FROM `".$this->setupdata['table_prefix']."catalog` `t1` , `".$this->setupdata['table_prefix']."discribe` `t2`
				 WHERE `t1`.`id` = ".$posid." AND `t2`.`type_of_value` = `t1`.`type` AND `t1`.`lang`='".strtolower($_SESSION['lang'])."'
				";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			$row1 = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$y = $row1['parent'];
			$sql = $this->mysqlclass->mysql_my_query("SELECT max(pos) FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$y." and lang ='".strtolower($_SESSION['lang'])."'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if ($row1['pos'] >= $row['max(pos)']) return $y;
			$type = $this->gettreetypes($row1['type']);
			$types = "('"; foreach ($type as $value) $types .= $value['type_of_value']."', '"; $types .= "')";
			$sql=$this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$y." and type IN ".$types." and pos > ".$row1['pos']." and lang ='".strtolower($_SESSION['lang'])."' ORDER BY `pos` ASC", __LINE__, __FILE__);
			$row2 = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if ((isset($row1['pos'])) and (isset($row2['pos'])) and (isset($row2['pos'])) and (isset($row1['pos'])) and (strlen($row1['pos'])>0) and (strlen($row2['pos'])>0)){
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$row2['pos'].", time_modif='$dat' WHERE id = ".$row1['id'], __LINE__, __FILE__);
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$row1['pos'].", time_modif='$dat' WHERE id = ".$row2['id'], __LINE__, __FILE__);
			}
			$this->mysqlclass->mysql_my_free_result($sql);
			return $y;
		}
		if ($do == 'up') {
			$query="
				SELECT 
				`t1` . * , `t2`.`type` FROM `".$this->setupdata['table_prefix']."catalog` `t1` , `".$this->setupdata['table_prefix']."discribe` `t2`
				 WHERE `t1`.`id` = ".$posid." AND `t2`.`type_of_value` = `t1`.`type` AND `t1`.`lang`='".strtolower($_SESSION['lang'])."'
				";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);			
			$row1 = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$y = $row1['parent'];
			if ($row1['pos'] <= 0) return $y;
			$type = $this->gettreetypes($row1['type']);
			$types = "('"; foreach ($type as $value) $types .= $value['type_of_value']."', '"; $types .= "')";
			$sql=$this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE parent=".$y." and type IN ".$types." and pos < ".$row1['pos']." and lang ='".strtolower($_SESSION['lang'])."' ORDER BY `pos` DESC", __LINE__, __FILE__);
			$row2 = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if ((isset($row1['pos'])) and (isset($row2['pos'])) and (isset($row2['pos'])) and (isset($row1['pos'])) and (strlen($row1['pos'])>0) and (strlen($row2['pos'])>0)){
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$row2['pos'].", time_modif='$dat' WHERE id = ".$row1['id'], __LINE__, __FILE__);
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=".$row1['pos'].", time_modif='$dat' WHERE id = ".$row2['id'], __LINE__, __FILE__);
			}
			$this->mysqlclass->mysql_my_free_result($sql);
			return $y;
		}
	}

	function treenew($parent, $name, $text, $hide, $depth, $pos, $lang, $gr, $dlink, $author) {
		$name = addslashes($name);
		$dlink = preg_replace("/[^\w+\:+\/+\.+\-+]/", "/", $dlink);
		$dlink = preg_replace("/\:{2,}|\.{2,}/", "/", $dlink);
		$dlink = preg_replace("/\/{3,}/", "/", $dlink);
		$dlink = preg_replace("/^\W|\W*$/", "", $dlink);
		$dlink = preg_replace("/([^:])\/{2,}/", "$1/", $dlink);
		//$dlink = addslashes($dlink);
		$dat = date("YmdHis");
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."catalog` (`id`,`name`,`hide`,`pos`,`parent`, `depth`, `type`,`time_create`,`time_modif`,`lang`,`groupe`,`dlink`,`author`,`editor`)
				VALUES ('', '$name', '$hide', '$pos', '$parent', '$depth', '$text', '$dat', '$dat', '$lang', '$gr', '$dlink', '$author', '$author')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$insert_id = $this->mysqlclass->mysql_my_insert_id();
		
		return $insert_id;
	}

	function treemod($id, $name, $hide, $gr, $dlink, $author){
		$name = addslashes($name);
		$dlink = preg_replace("/[^\w+\:+\/+\.+\-+]/", "/", $dlink);
		$dlink = preg_replace("/\:{2,}|\.{2,}/", "/", $dlink);
		$dlink = preg_replace("/\/{3,}/", "/", $dlink);
		$dlink = preg_replace("/^\W|\W*$/", "", $dlink);
		$dlink = preg_replace("/([^:])\/{2,}/", "$1/", $dlink);
		$dat = date("YmdHis");
		$sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET name='$name', hide='$hide', time_modif='$dat', groupe='$gr', dlink='$dlink', editor='$author' WHERE id = $id";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}

	function treemodupdate($id, $author){
		$sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET time_modif='".date("YmdHis")."', editor='".$author."' WHERE id = ".$id;
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}

	function treemodname($id, $name, $author){
		$name = addslashes($name);
		$dat = date("YmdHis");
		$sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET name='".$name."', time_modif='".$dat."', editor='".$author."' WHERE id = ".$id;
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}

	function treehide($id, $hide){
		$dat = date("YmdHis");
		$sql = "UPDATE `".$this->setupdata['table_prefix']."catalog` SET hide='".$hide."', time_modif='".$dat."' WHERE id = ".$id;
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}

	function treeparent($id) {
		if ($id > 0){
			$sql = $this->mysqlclass->mysql_my_query("select type FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$type = array_merge($this->gettreetypes($row['type']),  $this->gettreetypes('tree'));
		} else {
			$type = $this->gettreetypes('tree');
		}
		$types = "('"; foreach ($type as $value) $types .= $value['type_of_value']."', '"; $types .= "')";
		$sql = $this->mysqlclass->mysql_my_query("select max(pos) FROM `".$this->setupdata['table_prefix']."catalog` WHERE type IN ".$types." and parent=".$id." and lang='".strtolower($_SESSION['lang'])."'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($row > 0){
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
		    	if (count($rows['max(pos)'])==0) return 0;
			return $rows[0];
		} else return 0;
	}

	function gettreeparent($id) {
		$sql = $this->mysqlclass->mysql_my_query("select parent FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
		$this->mysqlclass->mysql_my_free_result($sql);
	    if (count($rows['parent'])==0) return -1;
		return $rows[0];
	}

	function gettreetypes($typ) {
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE `type`='".$typ."' ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}
	
	function getelemenstypes() {
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE `group`='element' ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}

	function getchildtypes($id, $groupe) {
		$info = $this->get_item_info($id);
		$type = $info['type'];
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE `group`='".$groupe."' and `type`='".$type."' ORDER BY pos", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}

	function gettreegroup() {
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("select groupe FROM `".$this->setupdata['table_prefix']."catalog` GROUP BY `groupe`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $rows;
	}

	function delmodulesdata($id){
		$id = intval($id);
		if (isset($this->setupdata['modules'])){
			foreach ($this->setupdata['modules'] as $key=>$sub_array) {
				$class = $key."class";
				global $$class;
				if (class_exists($class) && (method_exists($$class, "del_tree_elements"))) {
					return eval("return \$$class -> del_tree_elements(\$id);");
				}
			}
		}
	}
	
	function childsdel($id){
		$id = intval($id);
		if ($id > 0) {
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."data` WHERE `parent`=".$id." AND `type`!='block'", __LINE__, __FILE__);
		}
	}
	
	function treedel2($id) {
		$id = intval($id);
		$submenu = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."catalog` WHERE $id = parent", __LINE__, __FILE__);
		while ( $submenurow = $this->mysqlclass->mysql_my_fetch_array($submenu) ){	
			$id = $submenurow['id'];
			$sql=$this->mysqlclass->mysql_my_query("SELECT `parent`, `pos` FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
			$n1 = $rows[0];	$n2 = $rows[1];
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."data` WHERE parent=".$id, __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET pos=pos-1 WHERE parent = $n1 and pos > $n2 and `type` != 'block'", __LINE__, __FILE__);
			$this->delmodulesdata($id);
			$this->treedel2($id);
		} 
		$this->mysqlclass->mysql_my_free_result($submenu);
	}

	function treedel($id) {
		$id = intval($id);
		$sql=$this->mysqlclass->mysql_my_query("SELECT `parent`, `pos` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);
		$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
		$n1 = $rows[0];	$n2 = $rows[1];
		if (($n1 != "") && ($n2 != "")) $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET `pos`=`pos`-1 WHERE `parent` = ".$n1." AND `pos` > ".$n2." AND `type` != 'block'", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."data` WHERE `parent`=".$id, __LINE__, __FILE__);
		$this->childsdel($id);
		$this->delmodulesdata($id);
		$menu = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent` = ".$id." AND `type` != 'block'", __LINE__, __FILE__);
		while ( $menurow = $this->mysqlclass->mysql_my_fetch_array($menu, MYSQL_ASSOC) ){	
			$id = $menurow['id'];
			$this->treedel2($id);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."data` WHERE `parent`=".$id, __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);
			$this->delmodulesdata($id);
		}
		$this->mysqlclass->mysql_my_free_result($menu);
		return $n1;
	}

	function treepath($mas, $id) {
		foreach($mas as $val){
			if ($val['id'] == $id) {
		    	array_unshift($this->tree_path, $val);
				$this->treepath($mas, $val['parent']['id']);
			}
		}
	}

	function sqlpath($id) {
		$mas = array();
		$info = $this->get_item_info($id);
		$mas[] = $info;
		while ($info['parent'] != 0) {
			$info = $this->get_item_info($info['parent']);
			$mas[]=$info;
		}      
		return array_reverse($mas);
	}

	function gettreesub($id, $count, $rowsub, $types, $hide){
		$submenu = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE ".$id." = parent and type in ".$types." ORDER BY pos", __LINE__, __FILE__);
		while ( $submenurow = $this->mysqlclass->mysql_my_fetch_array($submenu, MYSQL_ASSOC) ){	
//			
			$submenurow['name'] = stripslashes($submenurow['name']);
			$id = $submenurow['id'];
			if (strlen($submenurow['dlink'])>0){
				if (count($this->active_langs) > 1) $cur_lang = $submenurow['lang']."/";
				else $cur_lang = "";
				if (strpos($submenurow['dlink'], "http://") === false) $submenurow['url'] = $this->setupdata['base_href']."/".$cur_lang.$submenurow['dlink'];
				else $submenurow['url'] = $submenurow['dlink'];
			} else $submenurow['url'] = $this->setupdata['base_href']."/"."index.php?id=".$submenurow['id'];
			if ($submenurow['type'] == 'url01') {
				$info = $this->get_field_value($submenurow['id'], 'static_url');
				if ((isset($info)) and (strlen($info)>0)) $submenurow['url'] = $info;
				$info = $this->get_field_value($submenurow['id'], 'url_target');
				if ((isset($info)) and (strlen($info)>0)) $submenurow['target'] = $info;
			}
			if ($hide == 0) {
				array_push($this->mas, $submenurow);
				$count++;
  			    if ($id>0) $this->mas[count($this->mas)-1]['parent']=$this->get_parent($this->mas[count($this->mas)-1]['parent']);
				$this->gettreesub($id, $count, $rowsub, $types, $hide);
				$count = 1;			
			}
			elseif ($submenurow['hide'] == 1) {
				array_push($this->mas, $submenurow);
				$count++;
  			    if ($id>0) $this->mas[count($this->mas)-1]['parent']=$this->get_parent($this->mas[count($this->mas)-1]['parent']);
				$this->gettreesub($id, $count, $rowsub, $types, $hide);
				$count = 1;			
			}
		} 
	}

	function get_parent ($id) {	
		for ($i=0; $i<count($this->mas); $i++) {
			$this->raw_tree[$this->mas[$i]['id']]=$this->mas[$i];
		}
		return $this->raw_tree[$id];
	}
	

	function maketree($parent, $lang, $type, $hide=0){
		if ($parent == "") $parent = 0;
		$this->mas = array();
		$page_values = array();
		$types = "('";
		foreach ($type as $value) $types .= $value['type_of_value']."', '";
		$types .= "')";
		$temp = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE lang = '".$lang."' and type in ".$types." ORDER BY depth ASC, pos ASC", __LINE__, __FILE__);
		while ( $row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC) ) {
      if ($hide==0 || $row["hide"]==1)
      {
  			$row["name"] = stripslashes($row["name"]);
  			if (strlen($row['dlink'])>0) {
				if (count($this->active_langs) > 1) $cur_lang = $row['lang']."/";
				else $cur_lang = "";
				if (strpos($row['dlink'], "http://") === false) $row['url'] = $this->setupdata['base_href']."/".$cur_lang.$row['dlink'];
				else $row['url'] = $row['dlink'];
  			} else $row['url'] = $this->setupdata['base_href']."/"."index.php?id=".$row['id'];
  			if ($row['type'] == 'url01') {
  				//$info = $this->get_field_value($row['id'], 'static_url');
					$info = $this->get_field_value($row['id'], 'static_url');
					if ((isset($info)) and (strlen($info)>0)) $row['url'] = $info;
  				$info = $this->get_field_value($row['id'], 'url_target');
					if ((isset($info)) and (strlen($info)>0)) $row['target'] = $info;
  			}
  			$temp[ $row["id"] ] = $row;
  		}
		}
		$tree = array();
		$tree2 = array();
		$xparent = 0;
		foreach ($temp as $item) {
      $xparent = $item["parent"];
      if($xparent>0) { $item["parent"] = array(); $item["parent"] = $tree[$xparent]; } else $item["parent"] = 0;
      $tree[ $item["id"] ] = $item;
      $tree2[ $xparent ][ $item["id"] ] = $item;
    }


    $this->tree = $tree;
    $this->tree2 = $tree2;
    $this->tree3 = array();
    $this->maketreesort($parent);
    
    $this->mas = $this->tree3;
    unset($tree,$tree2,$this->tree,$this->tree2,$this->tree3);
	}
	
	function maketreesort($parent)
  {
    if ($this->tree2[$parent])
    {
    foreach ($this->tree2[$parent] as $item)
    {
        $this->tree3[] = $item;
        $this->maketreesort($item["id"]);
    }
    }
  }
	
	function save_cached_tree($tree, $type_of_cache="0", $path_to_cache_file="0", $type){
		if (($type_of_cache != "0") && ($path_to_cache_file!="0")){
			$handle = fopen($path_to_cache_file, "w");
			$result = var_export($tree, true); 
			fwrite($handle, "<?\n\$cached_tree = ");
			fwrite($handle, $result);
			fwrite($handle, ";\n?>");
			fclose($handle);
			if ($type == 'guest'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_cache_time'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_cache_time', '".time()."')", __LINE__, __FILE__);
				else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'tree_cache_time'", __LINE__, __FILE__);
			} elseif ($type == 'admin'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_admin_cache_time'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_admin_cache_time', '".time()."')", __LINE__, __FILE__);
				else $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'tree_admin_cache_time'", __LINE__, __FILE__);
			}
		} else {
			$tree = serialize($tree);
			$tree = addslashes($tree);
			if ($type == 'guest'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='cached_tree'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'cached_tree', '".$tree."')", __LINE__, __FILE__);
				else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".$tree."' WHERE `name` = 'cached_tree'", __LINE__, __FILE__);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_cache_time'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_cache_time', '".time()."')", __LINE__, __FILE__);
				else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'tree_cache_time'", __LINE__, __FILE__);
			} elseif ($type == 'admin'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='cached_admin_tree'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'cached_admin_tree', '".$tree."')", __LINE__, __FILE__);
				else $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".$tree."' WHERE `name` = 'cached_admin_tree'", __LINE__, __FILE__);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='tree_admin_cache_time'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'tree_admin_cache_time', '".time()."')", __LINE__, __FILE__);
				else $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'tree_admin_cache_time'", __LINE__, __FILE__);
			}
		}
	}
	
	function get_cached_tree($type_of_cache="0", $path_to_cache_file="0", $type){
		if (($type_of_cache != "0") && ($path_to_cache_file != "0")){
			if (file_exists($path_to_cache_file)) {
				require $path_to_cache_file;
				if (isset($cached_tree)) return $cached_tree;
				else return 0;
			} else {
				return 0;
			}
		} else {
			if ($type == 'guest'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='cached_tree'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 1){
					$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
					$rows['value'] = stripslashes($rows['value']);
					return unserialize($rows['value']);
				} else return 0;
			} elseif ($type == 'admin'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='cached_admin_tree'", __LINE__, __FILE__);
				if ($this->mysqlclass->mysql_my_num_rows($sql) == 1){
					$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
					$rows['value'] = stripslashes($rows['value']);
					return unserialize($rows['value']);
				} else return 0;
			}
		}
	}
	
	function unique_multi_array($array, $sub_key) { 
	  $target = array(); 
	  $existing_sub_key_values = array(); 
	  foreach ($array as $key=>$sub_array) { 
    	  if (!in_array($sub_array[$sub_key], $existing_sub_key_values)) { 
        	  $existing_sub_key_values[] = $sub_array[$sub_key]; 
	          $target[$key] = $sub_array; 
    	   } 
	   } 
	   return $target; 
	} 

	function get_id_by_dlink($str, $lang="", $tree=0){
		$str = preg_replace("/[^\w+\:+\/+\.+\-+]/", "/", $str);
		$str = preg_replace("/\:{2,}|\.{2,}/", "/", $str);
		$str = preg_replace("/\/{3,}/", "/", $str);
		$str = preg_replace("/^\W|\W*$/", "", $str);
		$str = preg_replace("/\+/", "", $str);
		$str = preg_replace("/([^:])\/{2,}/", "$1/", $str);

		if ($lang == "") $lang = $this->setupdata['default_languages'];
		$lang = strtolower($lang);
		$types3 = $this->gettreetypes('tree');
		$types_of_element = array(); 
		$sql9 = $this->mysqlclass->mysql_my_query("SELECT DISTINCT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE `group`='element'", __LINE__, __FILE__);
		while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql9, MYSQL_ASSOC)) array_push($types_of_element, $rows);
		$types2 = "('";
		foreach ($types3 as $value) $types2 .= $value['type_of_value']."', '";
		foreach ($types_of_element as $value) $types2 .= $value['type_of_value']."', '";
		$types2 .= "')";

		$query = "SELECT DISTINCT `t1`.`id`, `t1`.`parent`, `t1`.`type`
				FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."catalog` `t2`
				WHERE `t1`.`type` IN ".$types2." AND `t1`.`dlink` = '".$str."' AND `t1`.`lang` = '".$lang."' AND (`t1`.`parent` = `t2`.`id` OR `t1`.`parent`=0)
		";
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);

		if ($tree == 0){
			$this->maketree(0, $lang, $types3, 0);		
			$tree=$this->mas;
		}
		
		foreach ($types3 as $value) $types5[] = $value['type_of_value'];
		foreach ($types_of_element as $value) $types6[] = $value['type_of_value'];
		
		while ($rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
			if (in_array($rows['type'], $types5)) {
				foreach ($tree as $el) if ($el['id'] == $rows['id']) {
					$this->mysqlclass->mysql_my_free_result($sql);
					return $el['id'];
				}
			} else{
				if (in_array($rows['type'], $types6)) {
					$is_in_tree = $this->get_parent_by_element_id($rows['id'], $rows['type'], $types6);
					foreach ($tree as $el) if ($el['id'] == $is_in_tree['id']) {
						$this->mysqlclass->mysql_my_free_result($sql);
						return $rows['id'];
					}
				}
			}
		}
		$this->mysqlclass->mysql_my_free_result($sql);
		return 0;
	}
	
	/* begin Dariaus makaliones */
	
	function _get_site_settings ($id=0){
		$result = array();	
		$query="	SELECT ".$this->setupdata['table_prefix']."data.name, ".$this->setupdata['table_prefix']."data.type, ".$this->setupdata['table_prefix']."data.value, ".$this->setupdata['table_prefix']."data.id FROM `".$this->setupdata['table_prefix']."catalog` 
					LEFT JOIN `".$this->setupdata['table_prefix']."data` ON ".$this->setupdata['table_prefix']."data.parent=".$this->setupdata['table_prefix']."catalog.id	
					WHERE ".$this->setupdata['table_prefix']."catalog.type='site_settings01' && ".$this->setupdata['table_prefix']."catalog.lang='".$_SESSION['lang']."' ";
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);		
		while ($row[] = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
			$result[$row[count($row)-1]['name']]=htmlspecialchars(stripslashes($row[count($row)-1]['value']));
		}		
		$this->mysqlclass->mysql_my_free_result($sql);
		
		if (($id != 0) && ($this->setupdata['vars']['keywords'] == 1)){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."keywords` WHERE ".$id."=`parent`", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
				$row = $this->mysqlclass->mysql_my_fetch_assoc($sql);
				if (trim($row['title']) != "") $result['site_name01'] = $row['title'];
				if (trim($row['describe']) != "") $result['site_discribe01'] = $row['describe'];
				if (trim($row['keywords']) != "") $result['site_keywords01'] = $row['keywords'];
			}
		}
		
		return $result;
	}
	
	function _get_page_elements_with_content ($id, $hide=0) {
		$query="
			SELECT `t2`.`type_of_value`
			FROM `".$this->setupdata['table_prefix']."catalog` `t1`,`".$this->setupdata['table_prefix']."discribe` `t2`
			WHERE `t1`.`id`='".$id."' && `t2`.`group`='element' && `t1`.`type`=`t2`.`type`
		";
		$x=$this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
		for ($i=0; $i < $this->mysqlclass->mysql_my_num_rows($x); $i++) {
			$_tmp=$this->mysqlclass->mysql_my_fetch_assoc($x);
			$result[]=$_tmp['type_of_value'];
		}
		if (!isset($result) || count($result)<1) return false;
		/*
		$query="
			SELECT DISTINCT `t2`.*, `t1`.`dlink` `dlink`, `t1`.`type` `type`, `t1`.`lang` `lang`, `t1`.`name` `original_name`, `t1`.`hide`,`t1`.`id` `original_id`,`t1`.`parent` `original_parent`
			FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."data` `t2`	
			WHERE `t1`.`parent`='".$id."' && `t1`.`type` IN ('".implode("','",$result)."') && `t2`.`parent`=`t1`.`id` ORDER BY `t1`.`pos`
		";*/
		$query="
			SELECT DISTINCT `t2`.*, `t1`.`pos` as `pos`, `t1`.`id` as `parent`, `t1`.`dlink` `dlink`, `t1`.`type` `type`, `t1`.`lang` `lang`, `t1`.`name` `original_name`, `t1`.`hide`,`t1`.`id` `original_id`,`t1`.`parent` `original_parent`
			FROM `".$this->setupdata['table_prefix']."catalog` `t1`	
			LEFT OUTER JOIN `".$this->setupdata['table_prefix']."data` `t2` ON (`t2`.`parent`=`t1`.`id`)
			WHERE `t1`.`parent`='".$id."' && `t1`.`type` IN ('".implode("','",$result)."') ORDER BY `t1`.`pos`
		";				
		$z=$this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);	
		for ($i=0; $i < $this->mysqlclass->mysql_my_num_rows($z); $i++) {
			$tmp1 = $this->mysqlclass->mysql_my_fetch_assoc($z);
			if (strlen($tmp1['dlink'])>0) {
				if (count($this->active_langs) > 1) $cur_lang = $tmp1['lang']."/";
				else $cur_lang = "";
				if (strpos($tmp1['dlink'], "http://") === false) $tmp1['url'] = $this->setupdata['base_href']."/".$cur_lang.$tmp1['dlink'];
				else $tmp1['url'] = $tmp1['dlink'];
			} else $tmp1['url'] = $this->setupdata['base_href']."/"."index.php?id=".$tmp1['parent'];
			if ($tmp1['type'] == 'url01') {
				$info = $this->get_field_value($tmp1['parent'], 'static_url');
				if ((isset($info)) and (strlen($info)>0)) $tmp1['url'] = $info;
				$info = $this->get_field_value($tmp1['parent'], 'url_target');
				if ((isset($info)) and (strlen($info)>0)) $tmp1['target'] = $info;
			}
			$result1[]=$tmp1;		
		}
		if (!isset($result1) || count($result1)<1) return false;
		foreach ($result1 as $key=>$value){
			$result2[$value['parent']]['id']=$value['original_id'];
			$result2[$value['parent']]['name']=$value['original_name'];
			$result2[$value['parent']]['type']=$value['type'];
			$result2[$value['parent']]['name']=stripslashes($value['original_name']);
			$result2[$value['parent']]['url']=$value['url'];
			$result2[$value['parent']]['hide']=$value['hide'];	
			$result2[$value['parent']]['parent']=$value['original_parent'];
			$result2[$value['parent']][$value['name']]=stripslashes($value['value']);
			$result2[$value['parent']]['pos']=$value['pos'];
			if (($hide != 0) and ($value['hide'] == 0)) unset($result2[$value['parent']]); 
		}		
		$this->mysqlclass->mysql_my_free_result($z);
		$this->mysqlclass->mysql_my_free_result($x);
		return $result2;
	}
	/* end Dariaus makaliones */


	function get_field_value($id, $name){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."data` WHERE `name` = '".$name."' AND `parent` ='".$id."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$this->mysqlclass->mysql_my_free_result($sql);
		return $info['value'];
	}
	
	function get_field_values($name){
    $return = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `parent`,`value` FROM `".$this->setupdata['table_prefix']."data` WHERE `name` = '".$name."'", __LINE__, __FILE__);
    while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC))
    {
      $return [ $row["parent"] ] = $row["value"];
    }
		$this->mysqlclass->mysql_my_free_result($sql);
		return $return;
	}
	
	function corect_pos($id) {
		$id = intval($id);
		if ($id > 0){
			$sql1 = $this->mysqlclass->mysql_my_query("select type FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$id, __LINE__, __FILE__);
			$row1 = $this->mysqlclass->mysql_my_fetch_array($sql1, MYSQL_ASSOC);
			$type1 = $this->gettreetypes($row1['type']);
			$type2 = $this->gettreetypes('tree');
			$type = array_merge($type2, $type1);
		} else $type = $this->gettreetypes('tree');
		$types = "('"; foreach ($type as $value) $types .= $value['type_of_value']."', '"; $types .= "')";
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE type IN ".$types." and parent=".$id." and lang='".strtolower($_SESSION['lang'])."'", __LINE__, __FILE__);
		$pos = 1;
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
					$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET `pos`=".$pos." WHERE `id` = ".$row['id'], __LINE__, __FILE__);
					$pos = $pos + 1;
					$this->corect_pos($row['id']);
			}
		} else return false;
	}
	
	function browser_detection() {
		/*
		Script Source URI: http://TechPatterns.com/downloads/php_browser_detection.php
		Version 1.1.3 Copyright (C) 04 April 2004
		UpDate by Technostudija.com 26.10.2004
		*/
		$browser_name = '';
		$browser_number = '';
		$string_length = 8;
		$browser_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
		$a_browser_types[] = array('msie', true, 'ie' );
		for ($i = 0; $i < count($a_browser_types); $i++){
			$search_string = $a_browser_types[$i][0];
			$b_dom = $a_browser_types[$i][1];
			$browser_name = $a_browser_types[$i][2];
			if ((strpos($browser_user_agent, ')')) and
				(strlen($browser_user_agent) <= strpos($browser_user_agent, ')')+1) and 
				(strpos($browser_user_agent, $browser_name)) and
				(stristr($browser_user_agent, $search_string))
				){
				$start_pos = strpos($browser_user_agent, $search_string);
				$start_pos += strlen($search_string) + 1;
				for ($i = $string_length; $i > 0 ; $i--){
					if (is_numeric(substr($browser_user_agent, $start_pos, $i))){
						$browser_number = substr($browser_user_agent, $start_pos, $i);
						break;
					}
				}
			} else $browser_name = "";
		}
		$a_browser_info = array($browser_name, $browser_number);
		if (($a_browser_info[0] != 'ie') or (($a_browser_info[0] = 'ie') and ((float)($a_browser_info[1]) <= 5 ))) return 0;
		else return 1;
	}
	
	function get_styles(){
		$array = array();
		$file_name = $this->setupdata['smarty_guest_styles_template_file'];
		if (file_exists($file_name)){
			$lines = file($file_name);
			foreach ($lines as $line) {
				$line = trim($line);
				if (strlen($line) > 0){
					$tmp1 = explode("&", $line);
					$tmp2 = explode(":", $tmp1[1]);
					$tmp5 = array();
					foreach ($tmp2 as $tmp3) {
						$tmp4 = explode("=", $tmp3);
						$tmp5[$tmp4[0]] = $tmp4[1];
					}
					$array[$tmp1[0]] = $tmp5;
				}
			}
			return $array;
		} else return false;
	}
	
	function set_array_urlencode($array){
		$new_array = array();
		foreach ($array as $key=>$item) {
			if (is_array($item)){
				$new_array[$key] = $this->set_array_urlencode($item);
			} else $new_array[$key] = urlencode($item);
		}
		return $new_array;
	}
		
	function get_page_permision($user, $page){
		if (!isset($page) || strlen($page) == 0) return 1;
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id`=".$user." AND `page_id`=".$page." AND `module`=''", __LINE__, __FILE__);		
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		$this->mysqlclass->mysql_my_free_result($sql);
		if ($row == 0) return 0;
		else return 1;
	}
	
	function set_child_permision($parent, $page){
		if ((!isset($page)) || (strlen($page) == 0) || (!isset($parent)) || (strlen($parent) == 0)) return 1;
		$del = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `page_id` = ".$page, __LINE__, __FILE__);		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `page_id`=".$parent, __LINE__, __FILE__);		
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', ".$page.", ".$row['users_id'].", '')", __LINE__, __FILE__);
		}
		$this->mysqlclass->mysql_my_free_result($sql);
	}

	function set_page_permision($login, $array, $lang){
		if ($array == NULL) $array = array();
		if (is_array($array)){
			$id_array = array();
			$sql = $this->mysqlclass->mysql_my_query("SELECT `page_id` FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id` = ".$login." AND `module` = ''", __LINE__, __FILE__);		
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$sql2 = $this->mysqlclass->mysql_my_query("SELECT `id`, `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$row['page_id'], __LINE__, __FILE__);				
				if ($this->mysqlclass->mysql_my_num_rows($sql2) == 1) {
					$row2 = $this->mysqlclass->mysql_my_fetch_array($sql2, MYSQL_ASSOC);
					if ($row2['lang'] == $lang) $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id` = ".$login." AND `module` = '' AND ".$row2['id']."=`page_id`", __LINE__, __FILE__);		
				}
			}
			$type = $this->gettreetypes('tree');
			$types = "('";foreach ($type as $value) $types .= $value['type_of_value']."', '";$types .= "')";
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type` in ".$types." AND `lang`='".$lang."'", __LINE__, __FILE__);				
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) $id_array[$row['id']] = $row['id'];
			foreach ($id_array as $key=>$item){
				if (!(array_key_exists($key, $array))) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', ".$key.", ".$login.", '')", __LINE__, __FILE__);
			}
			if (!(array_key_exists(0, $array))) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', 0, ".$login.", '')", __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_free_result($sql);
		} elseif (is_int($array)){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `page_id`=".$array." AND `users_id`=".$login." AND `module`=''", __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', ".$array.", ".$login.", '')", __LINE__, __FILE__);
		}
	}
	
	function set_site_permision($login, $array){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id` = ".$login." AND `module` != ''", __LINE__, __FILE__);							
		if ($array == NULL) return 0;
		foreach ($array as $key=>$item){
			if ((isset($this->setupdata['modules'][$key]['is']) && ($this->setupdata['modules'][$key]['is'] == 1)) || (isset($this->setupdata['vars']['main_module']) && (in_array($key, $this->setupdata['vars']['main_module'])))){
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', 0, ".$login.", '".$key."')", __LINE__, __FILE__);
			}
		}
	}
		
	function array_key_exists_recursive($needle, $haystack) {
		foreach($haystack as $key=>$val) {
			if(is_array($val)) {	
				if($this->array_key_exists_recursive($needle,$val)) return 1;
			} elseif($val == $needle) return 1;
	  }
	  return 0;
	}
		
	function get_micro_time() {
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}

	function get_debug_info(){
		$create_time = round( ($this->get_micro_time() - _time_start), 5);
		$included_files = get_included_files();
		$included_count = count($included_files);
		$query_count    = count($this->mysqlclass->db_log);
		$totsize = 0;
		natsort($included_files);
		foreach($included_files as $fname) {
			$fsize    = round((filesize($fname)/1024), 3);
			$totsize += $fsize;
		}
		$page_buffer = round((ob_get_length()/1024), 3);
		$str = "<div style=\"padding: 5px; background: #FF0000; color: #FFFFFF; position: absolute; top: 0; left: 0; z-index: 999;\">Page Created in ";
		$str .= $create_time. " Seconds - ";
		$str .= "Queries: " . $query_count. " - ";
		$str .= " Files: " . $included_count . " - Files size: ";
		$str .= $totsize. " Kb - Raw HTML: ";
		$str .= $page_buffer." Kb - ";
		$str .= "Memory usage: " . round(memory_get_usage()/1024)." Kb ";
    $str .= "</div>";
		return $str;
	}
	
	function get_dir_size($dirName = '.') {
		$dir  = dir($dirName);
		$size = 0;
		while($file = $dir->read()) {
		   if ($file != '.' && $file != '..') {
			   if (is_dir($file)) {
				   $size += dirsize($dirName . '/' . $file);
			   } else {
				   $size += filesize($dirName . '/' . $file);
			   }
		   }
		}
		$dir->close();
		return $size;
	}
	
	function get_help_tips_status($lang){
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t2`.* FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."data` `t2`	
			WHERE `t1`.`type` = 'site_settings01' AND `t1`.`lang`='".$lang."' AND `t2`.`parent`=`t1`.`id` AND `t2`.`name` = 'site_tips01' 
		", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$this->mysqlclass->mysql_my_free_result($sql);
			if ($row['value'] == 'on') return 'on';
		}
		return 'off';
	}
	
	function save_lang_status($lang, $lang_on){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='".$lang."_status'", __LINE__, __FILE__);
		if ($lang_on != "") $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', '".$lang."_status', '".$lang_on."')", __LINE__, __FILE__);
		else $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', '".$lang."_status', '')", __LINE__, __FILE__);
		$this->active_langs=$this->get_active_langs();
	}
	
	function get_lang_status($params){
		if (isset($params['lang'])) $lang = $params['lang'];
		else $lang = "";
		$result = array();
		
		if ($lang == ""){
			$m_version = mysql_get_server_info();
			if (($m_version !== false) && (strpos($m_version, "4.1.7") === false) && (strpos($m_version, "4.1.17") === false)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.`name` , `".$this->setupdata['table_prefix']."local_vars`.`value` FROM `".$this->setupdata['table_prefix']."discribe` `t1` LEFT JOIN `".$this->setupdata['table_prefix']."local_vars` ON `".$this->setupdata['table_prefix']."local_vars`.`name` = CONCAT(`t1`.`name` , '_status') WHERE `t1`.`type` = 'lang'", __LINE__, __FILE__);
				while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
					if (($row['value'] == "on") || ($row['value'] == null)) $result[$row['name']] = "on";
				}
			} else {
				$langs = $this->gettreetypes('lang');
				foreach($langs as $lang){
					$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='".$lang['name']."_status'", __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0){
						$row = mysql_fetch_array($sql, MYSQL_ASSOC);
						if ($row['value'] == "on") $result[$lang['name']] = "on";
					} else $result[$lang['name']] = "on";
				}
			}
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='".$lang."_status'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				if ($row['value'] == "on") $result = "on";
			} else $result = "on";
		}
		return $result;
	}
	
	function array_sort($array, $key){ 
	   for ($i = 0; $i < sizeof($array); $i++) { 
		   $sort_values[$i] = $array[$i][$key]; 
	   } 
	   asort ($sort_values); 
	   reset ($sort_values); 
	   while (list ($arr_key, $arr_val) = each ($sort_values)) { 
			 $sorted_arr[] = $array[$arr_key]; 
	   } 
	   return $sorted_arr; 
	}
	
	function mod_super_select($parent, $name, $array){
		if( count($array) > 1 && !empty($array[1]['text']) && !empty($array[1]['value']) )
		{
			$selected = $array['selected'];
			unset($array['selected']);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `group`='super_select'", __LINE__, __FILE__);		
			foreach ($array as $value){
				$value['text'] = substr($value['text'], 0, 255); 
				$value['text'] = addslashes($value['text']);
				$value['text'] = trim($value['text']);
				if ($value['value'] == 'noid'){
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `value`='".$value['text']."' AND `group`='super_select'", __LINE__, __FILE__);
					if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', ".$parent.", '".$name."', '".$value['text']."', 'text', 0, 'super_select')", __LINE__, __FILE__);
				} else {
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `id`=".$value['value']." AND `group`='super_select'", __LINE__, __FILE__);
					if ($this->mysqlclass->mysql_my_num_rows($sql) == 0) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('".$value['value']."', ".$parent.", '".$name."', '".$value['text']."', 'text', 0, 'super_select')", __LINE__, __FILE__);
					elseif ($this->mysqlclass->mysql_my_num_rows($sql) == 1) $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."object_data` SET `value`='".$value['text']."', `id`=".$value['value']." WHERE `parent` = ".$parent." AND `name`='".$name."' AND `group`='super_select'", __LINE__, __FILE__);
				}
			}

			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$parent." AND `name`='".$name."' AND `value`='".$selected['text']."' AND `group`='super_select'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$id = $row['id'];
			} else $id = 0;
			
			$this->mysqlclass->mysql_my_free_result($sql);
			return $id;
		}
	}
	
	function optimize_tabes(){
		$do = 0;
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='db_optimize_time'", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
			$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if (time()-intval($rows['value']) > 86400) $do = 1;
		} else {
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', 'db_optimize_time', '".time()."')", __LINE__, __FILE__);
			$do = 1;
		}
		if ($do == 1){
			$res = $this->mysqlclass->mysql_my_query("SHOW TABLES"); 
			$dbs = "";
			while ($data = $this->mysqlclass->mysql_my_fetch_row($res)) $dbs .= "`".$data[0]."`,";
			$dbs = substr($dbs, 0, strlen($dbs)-1);    
			$this->mysqlclass->mysql_my_query("OPTIMIZE TABLE ".$dbs); 
		}
		$this->mysqlclass->mysql_my_free_result($sql);
	}
	
	function get_product_key(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row;
		$str = var_export($this->setupdata['table_prefix'], true)."<br>\r\n";
		$str .= var_export($this->setupdata['binary_path'], true)."<br>\r\n";
		$str .= var_export($this->setupdata['base_href'], true)."<br>\r\n";
		$str .= var_export($this->setupdata['send_error_to'], true)."<br>\r\n";
		$str .= var_export($this->setupdata['core_dir'], true)."<br>\r\n";
		if (isset($this->setupdata['modules'])) $str .= var_export($this->setupdata['modules'], true)."<br>\r\n";
		$str .= var_export($result, true);
		return $str;
	}
	
	function check_product_key(){
		$headers = "From: SUPPORT <".$this->setupdata['send_error_to'].">\r\n";
		$headers .= "Subject: COPYRIGHT ERROR - ".$this->setupdata['base_href']."\n"; 
		$headers .= "Reply-To: SUPPORT <".$this->setupdata['send_error_to'].">\r\n";
		$headers .= "X-Mailer: JustPageIt Mailer 0.1\n"; 
		$headers .= "Mime-Version: 1.0\n"; 
		if (isset($this->setupdata['product_key'])){
			$data = $this->get_product_key();
			if (crypt(md5($data), base64_decode($this->setupdata['product_key'])) == base64_decode($this->setupdata['product_key'])) return "OK";
			else {
				@mail($this->setupdata['send_error_to'], "COPYRIGHT ERROR - ".$this->setupdata['base_href'], "", $headers);
				return "BAD";
			}
		} else {
			@mail($this->setupdata['send_error_to'], "COPYRIGHT KEY NOT FOUND - ".$this->setupdata['base_href'], "", $headers);
			return "OK";
		}
	}
	
	function save_event($module, $type, $data){
		global $setupdata;
		if ($setupdata['local_vars'] > 18){
			$module = trim($module);
			$module = addslashes($module);
			$type = trim($type);
			$type = addslashes($type);
			$data = serialize($data);
			$data = addslashes($data);
			$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."events` (`id`, `date`, `module`, `type`, `data`) VALUES ('', '".date('YmdHis')."', '".$module."', '".$type."', '".$data."')", __LINE__, __FILE__);
			if ($sql) return $this->mysqlclass->mysql_my_insert_id();
			else return 0;
		}
	}
	
	function myPHPErrorHandler($errno, $errstr, $errfile, $errline){
		global $setupdata;
		$find = $setupdata['smarty_compile_dir'];
		$find2 = $setupdata['core_dir']."/smarty/";
		if (($errno != 8) && (strpos($errfile, $find) === false) && (strpos($errfile, str_replace("/", "\\", $find)) === false) &&
			(strpos($errfile, $find2) === false) && (strpos($errfile, str_replace("/", "\\", $find2)) === false)){
			$message = print_r("<pre>", true);
			$message .= print_r("PHP ERROR\n", true);
			if (isset($_SERVER['HTTP_REFERER'])) $message .=  print_r("Error url: ".$_SERVER['HTTP_REFERER']."<br>\r\n", true);
			if (isset($_SERVER['QUERY_STRING'])) $message .=  print_r("Error query: ".$_SERVER['QUERY_STRING']."<br>\r\n", true);
			if (isset($_SERVER['REMOTE_ADDR'])) $message .= print_r("Client IP: ".$_SERVER['REMOTE_ADDR']."\n", true);
			if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $message .= print_r("Client Proxy: ".$_SERVER['HTTP_X_FORWARDED_FOR']."\n", true);
			$message .= print_r("Error code: ".$errno."\n", true);
			$message .= print_r("Error report: ".$errstr."\n", true);
			$message .= print_r("Error in file: ".$errfile."\n", true);
			$message .= print_r("Error in line: ".$errline."\n", true);
			$message .= print_r("</pre>", true);
			if (isset($_POST) && (count($_POST) > 0)) {
				$message .= "<br>\r\n";
				$message .= "POST :<br>\r\n";
				foreach ($_POST as $key=>$value) {
					if (is_array($value)) $message .= "POST['".$key."'] = ".print_r($value, true)."<br>\r\n";
					$message .= "POST['".$key."'] = ".$value."<br>\r\n";
				}
			}
			if (isset($_GET) && (count($_GET) > 0)) {
				$message .= "<br>\r\n";
				$message .= "GET :<br>\r\n";
				foreach ($_GET as $key=>$value) {
					if (is_array($value)) $message .= "GET['".$key."'] = ".print_r($value, true)."<br>\r\n";
					else $message .= "GET['".$key."'] = ".$value."<br>\r\n";
				}
			}

			if (isset($setupdata['_debug_']) || (strpos($errstr, "Smarty error") !== false)){
				print_r($message);
				$this->mysqlclass->close_db();
				die();
			}

			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
			$headers .= "From: SUPPORT <".$setupdata['send_error_to'].">\r\n";
			$headers .= "X-Mailer: JustPageIt Mailer 0.2\r\n";
			@mail($setupdata['send_error_to'], "PHP ERROR - ".$_SERVER['SERVER_NAME']." - ".$setupdata['base_href'], $message, $headers);
			
			while (ob_get_level()) ob_end_clean();
			if (is_file($setupdata['smarty_admin_template_dir']."admin/phperror.tpl")){
				readfile($setupdata['smarty_admin_template_dir']."admin/phperror.tpl");
				print_r("<!--\n");
				print_r($message);
				print_r("\n-->");
			}
			$this->mysqlclass->close_db();
			die();
		}
	}
	
	function get_page_lang($id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE ".$id."=`id`", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$this->mysqlclass->mysql_my_free_result($sql);
			return $row['lang'];
		} else {
			return $this->setupdata['default_languages'];
		}
	}
	
	function create_google_site_map($file, $add=0){
		$error = array();
		$do = 0;
		if (is_file($file) && is_writable($file)) $do = 1;
		else {
			$fp = @fopen($file, "w");
			if ($fp){
				@fclose($fp);
				$do = 1;
				@unlink($file);
			} else $error['permission_error'] = "permission_error";
		}
		if ($do == 1){
			$do = 0;
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='create_google_site_map'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0) {
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				if (time()-intval($rows['value']) > 86400) $do = 1;
			} else {
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', 'create_google_site_map', '".time()."')", __LINE__, __FILE__);
				$do = 1;
			}
			if ($add != 0) $do = 1;
			if ($do == 1){
				$handle = fopen($file, 'w');
				fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>'."\n");
				fwrite($handle, '<!-- generator="JustPageIt" -->'."\n");
				fwrite($handle, '<!-- sitemap-generator-url="http://www.justpageit.lt" sitemap-generator-version="0.0.1"  -->'."\n");
				fwrite($handle, '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">'."\n");
				
				$types = "(";
				$t1 = $this->gettreetypes('tree');
				foreach ($t1 as $v1){
					$types .= "'".$v1['type_of_value']."', ";
					$t2 = $this->gettreetypes($v1['type_of_value']);
					foreach ($t2 as $v2){
						$types .= "'".$v2['type_of_value']."', ";
					}
				}
				$types = trim($types);
				if (strlen($types) > 1) $types = substr($types, 0, strlen($types)-1);
				$types .= ")";
				
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `dlink`, UNIX_TIMESTAMP(`time_modif`) as `date`, `depth`, `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type` in ".$types." AND `hide`=1", __LINE__, __FILE__);				
				while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
					fwrite($handle, '	<url>'."\n");
					if (strlen($row['dlink'])>0) {
						if (count($this->active_langs) > 1) $cur_lang = $row['lang']."/";
						else $cur_lang = "";
						if (strpos($row['dlink'], "http://") === false) $url = $this->setupdata['base_href']."/".$cur_lang.$row['dlink'];
						else $url = $row['dlink'];
					} else {
						$url = $this->setupdata['base_href']."/"."index.php?id=".$row['id'];
					}
					$date_mod = date("O", $row['date']);
					$date_mod = substr($date_mod, 0, 3).":".substr($date_mod, 3, 4);
					$url = str_replace(array('&','"',"'",'<','>'), array('&amp;','&quot;','&apos;','&lt;','&gt;'), $url);
					fwrite($handle, '		<loc>'.$url.'</loc>'."\n");
					fwrite($handle, '		<lastmod>'.date("Y-m-d\TH:i:s", $row['date']).$date_mod.'</lastmod>'."\n");
					fwrite($handle, '		<changefreq>daily</changefreq>'."\n");
					$row['depth'] = $row['depth'] + 1;
					$priority = round(1/$row['depth'], 2);
					fwrite($handle, '		<priority>'.$priority.'</priority>'."\n");
					fwrite($handle, '	</url>'."\n");
				}
				fwrite($handle, '</urlset>'."\n");
				fclose($handle);
				$handle = @fopen($file, "rb");
				if ($handle) $error['create_file_ok'] = "create_file_ok";
				else $error['create_file_error'] = "create_file_error";
				$url = $this->setupdata['base_href']."/sitemap.xml";
				$url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$url;
				$handle = @fopen($url, "rb");
				if ($handle) $error['send_file_ok'] = "send_file_ok";
				else $error['send_file_error'] = "send_file_error";
				fclose($handle);
				$this->mysqlclass->mysql_my_free_result($sql);
			}
		}
		return $error;
	}
	
	function search_in_tree($str, $array){
		$result = $array;
		foreach ($array as $k=>$v){
			if (stristr($v['name'], $str) === false) unset($result[$k]);
		}
		return $result;
	}
	
	function shutdownscript(){
		//$this->mysqlclass->mysql_my_free_result();
		global $mysqlclass;
		$mysqlclass->close_db();
	}
	
	function save_keywords(){
		if (isset($_POST['parent']) && intval($_POST['parent']) > 0) {
			$id = intval($_POST['parent']);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."keywords` WHERE ".$id."=`parent`", __LINE__, __FILE__);
			if (isset($_POST['page_title'])) $t = mysql_real_escape_string($_POST['page_title']); else $t = "";
			if (isset($_POST['page_describe'])) $d = mysql_real_escape_string($_POST['page_describe']); else $d = "";
			if (isset($_POST['page_keywords'])) $k = mysql_real_escape_string($_POST['page_keywords']); else $k = "";
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."keywords` (`parent`,`title`,`describe`,`keywords`) VALUES ('".$id."',  '".$t."',  '".$d."',  '".$k."')", __LINE__, __FILE__);
		}
	}
	
}
?>
